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();
}
}
}
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();
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);
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
After saving the video, when I check, I get an empty video in the gallery
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…