android-将视频压缩到firebase存储
Posted
技术标签:
【中文标题】android-将视频压缩到firebase存储【英文标题】:android- compress video to firebase storage 【发布时间】:2018-01-19 00:16:47 【问题描述】:我正在尝试压缩从图库中挑选的视频文件,使用 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】:将以下代码放入AsyncTask
String compressFilePath = "";
try
compressFilePath = SiliCompressor.with(MainActivity.this).compressVideo(filepath.toString(), mDatabase.toString());
catch (URISyntaxException e)
e.printStackTrace();
Log.v("Error", e.getMessage());
【讨论】:
以上是关于android-将视频压缩到firebase存储的主要内容,如果未能解决你的问题,请参考以下文章
Android - 将视频/音频文件上传到 Firebase 存储
如何在将图像上传到 Firebase 存储之前对其进行压缩?
Android Glide 和 Firebase 存储:将连续图像从 Firebase 存储加载到 Imageview - 闪烁