Android Java压缩从图库中选择的视频

问题描述

我想选择一个视频并将其压缩,以便将其上传到Firebase存储。

我已经尝试了多个库,但到目前为止还没有成功。

到目前为止我所取得的成就

-打开意图 选择视频 -获取选定的视频uri -将其上传到存储空间

缺少什么 -在上传视频之前先对其进行压缩

我尝试了silicompressor,但无法使其与我在网上找到的所有内容一起使用

 if (data != null) {

            try {
                String filePath = SiliCompressor.with(this).compressVideo(data.getData(),Environment.getExternalStorageState());


                MediaMetadataRetriever retriever = new MediaMetadataRetriever();
                retriever.setDataSource(this,data.getData());
                String time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
                long timeInMilliSec = Long.parseLong(time);
                long duration = timeInMilliSec / 1000;
                long hours = duration / 3600;
                long minutes = (duration - hours * 3600) / 60;
                long seconds = duration - (hours * 3600 + minutes * 60);

                if (seconds <= 30) {
                    ratio = 1f;
                    rightNavButton.setEnabled(true);
                    rightNavButton.setColorFilter(ContextCompat.getColor(NewPostActivity.this,R.color.AppColor));
                    titleView.setText(R.string.ready_to_share);
                    videoUrl = Uri.parse(filePath);
                } else {
                    Toast.makeText(this,R.string.video_max_length,Toast.LENGTH_SHORT).show();
                }

            } catch (URISyntaxException e) {
                e.printStackTrace();
                Log.d("yyyy","onActivityResult: " + e.getMessage());
            }
        }

我的上传文件

    public void sharePostVideo(final Context context,final Uri videoUrl,String id,final Interfaces.OnSuccessString onSuccess) throws IOException {
    storageReference = FirebaseStorage.getInstance().getReference();
    final StorageReference filepath = storageReference.child(STORAGE_VIDEO).child(id);

    filepath.putFile(videoUrl).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

            filepath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                @Override
                public void onSuccess(Uri uri) {
                    onSuccess.onSuccess(uri.toString());
                    Log.d("yyyy","onSuccess: ");

                }
            });
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            Log.d("yyyy","onFailure: " + e.getMessage());
        }
    });
}

在进行上传工作之前,如何以及在哪里缩小视频的大小?

编辑:

如果我使用

videoUrl = data.getData()

成功上传 如果我使用上面显示的代码不起作用

E/StorageException: StorageException has occurred.
An unknown error occurred,please check the HTTP result code and inner exception for server response.

E/StorageException: No content provider: mounted/VIDEO_20201003_211227.mp4
java.io.FileNotFoundException: No content provider: mounted/VIDEO_20201003_211227.mp4

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...