微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用 Video Uri 在 Android 上压缩视频

如何解决如何使用 Video Uri 在 Android 上压缩视频

我想压缩我的视频,然后将其上传到 firebase。但是如何使用 Uri 压缩视频,这里是我的上传活动代码在这里我制作了一种将视频上传到 firebase 存储的方法,但我希望首先压缩视频,然后上传到 firebase。这里的任何人都请帮助我。

public class Upload_reels extends AppCompatActivity {
    private ImageView plus;
    private VideoView reelvideo;
    private EditText captiontxt;
    private Button uploadbtn;
    private RelativeLayout picktbtn;
    public static final int CODE = 12;
    private DatabaseReference reference;
    private FirebaseDatabase database;
    private FirebaseAuth firebaseAuth;
    private StorageReference storageReference;
    private FirebaseStorage firebaseStorage;
    private Uri videoUri;
    String profileUrl,tick,username,captionst,timestamp;
    private ProgressDialog progressDialog;
    String videoUrl,compressedUri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_upload_reels);

        getSupportActionBar().hide();

        plus = findViewById(R.id.plusicon);
        reelvideo  = findViewById(R.id.reelPlay);
        captiontxt = findViewById(R.id.caption);
        uploadbtn = findViewById(R.id.upload_reels_btn);
        picktbtn = findViewById(R.id.pickvideobtn);

        firebaseAuth = FirebaseAuth.getInstance();
        database = FirebaseDatabase.getInstance();
        reference = database.getReference("Users");
        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("Uploading.....");
        GiraffeCompressor.init(this);



        reference.child(firebaseAuth.getCurrentUser().getUid()).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                if (snapshot.exists()) {

                    profileUrl = snapshot.child("ProfileImage").getValue().toString();
                    username = snapshot.child("Name").getValue().toString();
                    tick = snapshot.child("Tick").getValue().toString();

                    Toast.makeText(Upload_reels.this,Toast.LENGTH_SHORT).show();


                }

            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });



        picktbtn.setonClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                pichvideo();
            }
        });

        uploadbtn.setonClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                captionst = captiontxt.getText().toString().trim();
                checkutills();
            }
        });






    }

    private void checkutills() {

        if (TextUtils.isEmpty(captionst)){
            Toast.makeText(this,"Please Enter a best Caption for your video",Toast.LENGTH_SHORT).show();
        }

        else {

            uploadvideotostorage();
        }


    }

    private void uploadvideotostorage() {
        progressDialog.show();

        if (videoUri!=null){

            timestamp = String.valueOf(System.currentTimeMillis());
            String filepathname = "Post/" + "post_" + timestamp;

            firebaseStorage = FirebaseStorage.getInstance();
            storageReference = firebaseStorage.getReference("Reels").child(filepathname);
            storageReference.putFile(videoUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                    taskSnapshot.getStorage().getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                        @Override
                        public void onSuccess(Uri uri) {

                            videoUrl = String.valueOf(uri);

                            addtodatabse();

                        }
                    });

                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {

                }
            });





        }
    }

    private void addtodatabse() {

        HashMap<String,String> hashMap = new HashMap<>();
        hashMap.put("videoUrl",videoUrl);
        hashMap.put("tick",tick);
        hashMap.put("Caption",captionst);
        hashMap.put("Username",username);
        hashMap.put("profileurl",profileUrl);
        hashMap.put("pId",timestamp);
        hashMap.put("UID",firebaseAuth.getCurrentUser().getUid());


        DatabaseReference refdata = FirebaseDatabase.getInstance().getReference("TravelReels");
        refdata.child(timestamp).setValue(hashMap).addOnSuccessListener(new OnSuccessListener<Void>() {
            @Override
            public void onSuccess(Void aVoid) {

                Toast.makeText(Upload_reels.this,"Reels Uploaded Successfully",Toast.LENGTH_SHORT).show();
                progressDialog.dismiss();

            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Toast.makeText(Upload_reels.this,e.getMessage(),Toast.LENGTH_SHORT).show();
                progressDialog.dismiss();

            }
        });




    }

    private void pichvideo() {

        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setType("video/*");
        startActivityForResult(intent,CODE);
    }

    @Override
    protected void onActivityResult(int requestCode,int resultCode,@Nullable Intent data) {
        super.onActivityResult(requestCode,resultCode,data);

        if (requestCode == CODE && resultCode == RESULT_OK && data!=null){

            reelvideo.setVisibility(View.VISIBLE);
            plus.setVisibility(View.GONE);

            videoUri = data.getData();

            reelvideo.setVideoURI(videoUri);



            reelvideo.start();


        }
    }



}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。