【发布时间】:2020-01-15 16:19:49
【问题描述】:
要录制 SurfeceView 我正在使用第 3 方 library ,这个库需要一个路径,在我的案例中保存的输出(录制的视频)是 savedVideoPath :: p>
mRenderPipeline = EZFilter.input(this.effectBmp)
.addFilter(new Effects().getEffect(VideoMaker.this, i))
.enableRecord(savedVideoPath, true, false)
.into(mRenderView);
录制停止后,应该以savedVideoPath为路径保存视频,当我测试代码时,也就是说,当我打开图库应用时,我看到保存的视频在那里,但是当我在 Android Q 上测试时,我什么都看不到。
由于getExternalStoragePublicDirectory 和getExternalStorageDirectory 已被弃用,我尝试使用getExternalFilesDir 如下:
private void getPath() {
String videoFileName = "video_" + System.currentTimeMillis() + ".mp4";
fileName = videoFileName;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
File imageFile = null;
File storageDir = new File(
getExternalFilesDir(Environment.DIRECTORY_MOVIES),
"Folder");
source = storageDir;
boolean success = true;
if (!storageDir.exists()) {
success = storageDir.mkdirs();
}
if (success) {
imageFile = new File(storageDir, videoFileName);
savedVideoPath = imageFile.getAbsolutePath();
}
} else {
File storageDir = new File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES)
+ "/Folder");
boolean success = true;
if (!storageDir.exists()) {
success = storageDir.mkdirs();
}
if (success) {
File videoFile = new File(storageDir, videoFileName);
savedVideoPath = videoFile.getAbsolutePath();
}
}
}
录制停止后,我转到 Files Explorer app > Android > data > com.packageName > files > Movies > Folder ,我可以在那里看到所有保存的视频,但我看不到他们在画廊里。
我尝试使用Intent.ACTION_MEDIA_SCANNER_SCAN_FILE 刷新图库,但很遗憾不起作用。
我也试过MediaScannerConnection:
MediaScannerConnection.scanFile(
context,
new String[]{savedVideoPath},
new String[]{"video/mp4"},
new MediaScannerConnection.MediaScannerConnectionClient() {
public void onMediaScannerConnected() {
}
public void onScanCompleted(String s, Uri uri) {
}
});
- 谁能帮我解决这个问题?我坚持了将近 2 天
【问题讨论】:
-
试试
MediaScannerConnection和它的scanFile()方法。请注意,您可能无法使用单个文件同时满足这两个条件(具有文件系统访问权限并且视频出现在MediaStore中以供图库应用程序使用)。 -
@CommonsWare,感谢您的评论,我已经尝试过了,但不幸的是,在图库中找不到视频
-
该库可能适合使用the
MediaMuxerconstructor that takes aFileDescriptor。然后,您将能够在ContentResolver上使用openFileDescriptor()以返回您的MediaStoreUri问题。否则,在您使用库修改视频后,将其复制到MediaStore(请参阅您之前的问题)并删除您的文件副本。 -
@CommonsWare 感谢您的评论,是复制路径还是文件本身? ,以及如何将其复制到
MediaStore,谢谢 -
"复制路径还是文件本身?" - 文件。 “我如何将它复制到 MediaStore”——我们在 an earlier question 中讨论过这个问题。您在那里的代码以及我的答案中的修改应该可以正常工作......只需使用转换后的视频文件作为数据源。 This class 表示正在下载视频;您的代码将是相同的,只是使用一个文件作为您的数据源。
标签: java android android-external-storage android-mediascanner android-10.0