在Android 10上将视频保存并插入图库 [英] Save and Insert video to gallery on Android 10
问题描述
我正在尝试将视频保存到图库中,以下代码在除 Android Q 以外的所有Android版本上均能正常运行:
I'm trying to save a video to the gallery,the following code works well an all Android versions except the Android Q:
private void getPath() {
String videoFileName = "video_" + System.currentTimeMillis() + ".mp4";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
ContentResolver resolver = getContentResolver();
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, videoFileName);
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "video/mp4");
contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, "Movies/" + "Folder");
contentValues.put(MediaStore.Video.Media.IS_PENDING, 1);
Uri collection = MediaStore.Video.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);
Uri videoUri = resolver.insert(collection, contentValues);
if (videoUri != null) {
try (ParcelFileDescriptor pfd = resolver.openFileDescriptor(videoUri, "w", null)) {
OutputStream outputStream = null;
if (pfd != null) {
outputStream = resolver.openOutputStream(videoUri);
outputStream.flush();
outputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
contentValues.clear();
contentValues.put(MediaStore.Video.Media.IS_PENDING, 0);
if (videoUri != null) {
resolver.update(videoUri, contentValues, null, null);
}
} 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();
}
}
}
我也尝试使用get getExternalFilesDir()
,但不起作用,画廊中没有创建视频
I also tried using get getExternalFilesDir()
, but doesn't work, no video created in the gallery
String videoFileName = "video_" + System.currentTimeMillis() + ".mp4";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
File imageFile = null;
File storageDir = new File(getExternalFilesDir(Environment.DIRECTORY_DCIM),
"Folder");
boolean success = true;
if (!storageDir.exists()) {
success = storageDir.mkdirs();
}
if (success) {
imageFile = new File(storageDir, videoFileName);
}
savedVideoPath = imageFile.getAbsolutePath();
我使用第三方库记录 SurfaceView
,此库需要一个路径来保存录制的视频:
I use a 3rd-party library to record SurfaceView
, this library requires a path to save the recorded video :
mRenderPipeline = EZFilter.input(this.effectBmp)
.addFilter(new GalleryEffects().getEffect(VideoMaker.this, i))
.enableRecord(savedVideoPath, true, false)
.into(mRenderView);
录制视频完成后,录制的视频将以给定路径 savedVideoPath
,除了 Android Q
When record video finished, the recorded video saved with the given path savedVideoPath
, everything works just fine on all android version except the Android Q
之外,所有版本在所有Android版本上都可以正常运行,保存视频后,当我检查时,我在图库中看到一个空视频
After saving the video, when I check, I get an empty video in the gallery
推荐答案
我想创建一个使用它的路径
I want to create a path to use it
您从 MediaStore
获取 Uri
。没有路径。 Uri
不仅不能转换为路径,而且您无法在Android 10及更高版本上访问该位置的文件系统。
You are getting a Uri
from MediaStore
. There is no "path". Not only can a Uri
not be converted to a path, but you do not have filesystem access to that location on Android 10 and higher.
摆脱掉:
if (videoUri != null) {
savedVideoPath = getRealPathFromURI(videoUri);
}
,因为它不起作用。
将其替换为代码,将视频写到 Uri
所标识的位置。使用 resolver.openOutputStream()
将 OutputStream
移至该位置。特别是,在调用 resolver.update()
的之前,执行 IS_PENDING
的操作 0
,具体说:我已经写完 Uri
;您现在可以使用内容了。
Replace it with your code to write out your video to the location identified by the Uri
. Use resolver.openOutputStream()
to get an OutputStream
to that location. In particular, do this before you call resolver.update()
for an IS_PENDING
of 0
, as that specifically says "I am done writing to the Uri
; you can use the content now".
或者,使用您有权访问的文件系统位置之一,例如 getExternalFilesDir()$在
上下文
上使用c $ c>,并删除 MediaStore
之类的东西。
Or, use one of the filesystem locations that you do have access to, such as getExternalFilesDir()
on Context
, and get rid of the MediaStore
stuff.
这篇关于在Android 10上将视频保存并插入图库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!