android-压缩视频到firebase存储

时间:2017-08-10 16:24:45

标签: android firebase video compression firebase-storage

我尝试压缩从图库中挑选的视频文件,使用SiliCompressor(https://github.com/Tourenathan-G5organisation/SiliCompressor)对其进行压缩并上传到FirebaseStorage。问题是压缩视频的文件路径无效。我如何压缩视频并将其上传到FirebaseStorage?我会很高兴得到任何帮助,谢谢。

    uploadBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent pickIntent = new Intent();
            pickIntent.setAction(Intent.ACTION_GET_CONTENT);
            pickIntent.setType("video/*");
            startActivityForResult(Intent.createChooser(pickIntent, "Pick video"), RC_PICK_VIDEO);
        }
    });


        @Override
protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RC_PICK_VIDEO && resultCode == RESULT_OK && data != null){
        final Uri filepath = data.getData();
        final String name = "VIDEO" + filepath.getLastPathSegment().toString() + ".mp4";

        mDatabaseReference = mDatabase.push();

        String compressFilePath = "";
        try {
            compressFilePath = SiliCompressor.with(MainActivity.this).compressVideo(filepath.toString(), mDatabase.toString());

        } catch (URISyntaxException e) {
            e.printStackTrace();
            Log.v("Error", e.getMessage());
        }
        StorageReference videoRef = mStorageReference.child(compressFilePath);

        videoRef.putFile(Uri.parse(compressFilePath)).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                Uri downloadedUrl = taskSnapshot.getDownloadUrl();

                String url = downloadedUrl.toString();

                getDownloadedUrl(url);   

                uploadToDatabase(name, url);

            }
        }).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
                int progress = (int) ((100 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount());
                Toast.makeText(getApplicationContext(), "Upload is " + progress + "% done", Toast.LENGTH_LONG).show();

            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Toast.makeText(getApplicationContext(), "Error: " + e.getMessage(), Toast.LENGTH_LONG).show();
                Log.v("Error: ", e.getMessage());
            }
        });

    }
}

压缩视频的结果文件路径无效。当我打开链接时,应该保存压缩视频,没有什么。

我的logcat是:

    08-10 17:48:49.256 10009-10009/com.example.admin.databaseproject E/tmessages: https:/firechat-99dd1.firebaseio.com/VideoItem/VIDEO_20170810_174849.mp4: open failed: ENOENT (No such file or directory)
    08-10 17:48:49.256 10009-10009/com.example.admin.databaseproject E/tmessages: time = 0
    08-10 17:48:49.256 10009-10009/com.example.admin.databaseproject E/ViratPath: /storage/sdcard0/Movies/Instagram/VID_22750407_161649_344.mp4
    08-10 17:48:49.256 10009-10009/com.example.admin.databaseproject E/ViratPath: https:/firechat-99dd1.firebaseio.com/VideoItem/VIDEO_20170810_174849.mp4
    08-10 17:48:49.256 10009-10009/com.example.admin.databaseproject E/ViratPath: /storage/sdcard0/Movies/Instagram/VID_22750407_161649_344.mp4
    08-10 17:48:49.258 10009-10009/com.example.admin.databaseproject E/UploadTask: could not locate file for uploading:https:/firechat-99dd1.firebaseio.com/VideoItem/VIDEO_20170810_174849.mp4
    08-10 17:48:49.258 10009-10009/com.example.admin.databaseproject E/StorageException: StorageException has occurred.
                                                                                 An unknown error occurred, please check the HTTP result code and inner exception for server response.
                                                                                  Code: -13000 HttpResult: 0
    08-10 17:48:49.259 10009-10009/com.example.admin.databaseproject E/StorageException: No content provider: https:/firechat-99dd1.firebaseio.com/VideoItem/VIDEO_20170810_174849.mp4
                                                                                 java.io.FileNotFoundException: No content provider: https:/firechat-99dd1.firebaseio.com/VideoItem/VIDEO_20170810_174849.mp4
                                                                                     at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1066)
                                                                                     at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:921)
                                                                                     at android.content.ContentResolver.openInputStream(ContentResolver.java:646)
                                                                                     at com.google.firebase.storage.UploadTask.<init>(Unknown Source)
                                                                                     at com.google.firebase.storage.StorageReference.putFile(Unknown Source)
                                                                                     at com.example.admin.databaseproject.MainActivity.onActivityResult(MainActivity.java:137)
                                                                                     at android.app.Activity.dispatchActivityResult(Activity.java:6235)
                                                                                     at android.app.ActivityThread.deliverResults(ActivityThread.java:3584)
                                                                                     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3631)
                                                                                     at android.app.ActivityThread.access$1300(ActivityThread.java:151)
                                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359)
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                     at android.os.Looper.loop(Looper.java:135)
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:5268)
                                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                                     at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697)

1 个答案:

答案 0 :(得分:0)

将以下代码放在AsyncTask

     String compressFilePath = "";
    try {
        compressFilePath = SiliCompressor.with(MainActivity.this).compressVideo(filepath.toString(), mDatabase.toString());

    } catch (URISyntaxException e) {
        e.printStackTrace();
        Log.v("Error", e.getMessage());
    }