Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
555 views
in Technique[技术] by (71.8m points)

java - Save and Insert video to gallery on Android 10

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

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I have answered you to your other post to... You need an inputstream (file, bitmap etc.) and write an outputstream from the inputfile.

You have to change the library to make it work with Android Q . If you cannot do this you could copy the video to the media gallery and then delete the old video created in getExternalFilesDir(). After this you have the uri of the video and can do what you want with the uri

If you have saved the video with getExternalFilesDir() you could use my example here : The media uri you get is "uriSavedVideo" . This is only an example. A large video should also be copied in the background.

String videoFileName = "video_" + System.currentTimeMillis() + ".mp4";

ContentValues valuesvideos;
valuesvideos = new ContentValues();
valuesvideos.put(MediaStore.Video.Media.RELATIVE_PATH, "Movies/" + "Folder");
valuesvideos.put(MediaStore.Video.Media.TITLE, videoFileName);
valuesvideos.put(MediaStore.Video.Media.DISPLAY_NAME, videoFileName);
valuesvideos.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4");
valuesvideos.put(
    MediaStore.Video.Media.DATE_ADDED, 
    System.currentTimeMillis() / 1000);
valuesvideos.put(MediaStore.Video.Media.DATE_TAKEN, System.currentTimeMillis());
valuesvideos.put(MediaStore.Video.Media.IS_PENDING, 1);

ContentResolver resolver = mContext.getContentResolver();
Uri collection = 
    MediaStore.Video.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);
Uri uriSavedVideo = resolver.insert(collection, valuesvideos);
ParcelFileDescriptor pfd;

try {
    pfd = mContext.getContentResolver().openFileDescriptor(uriSavedVideo, "w");

    FileOutputStream out = new FileOutputStream(pfd.getFileDescriptor());
    // Get the already saved video as fileinputstream from here
    File storageDir = new File(
        mContext.getExternalFilesDir(Environment.DIRECTORY_MOVIES), 
        "Folder");
    File imageFile = new File(storageDir, "Myvideo");
    FileInputStream in = new FileInputStream(imageFile);

    byte[] buf = new byte[8192];
    int len;
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }

    out.close();
    in.close();
    pfd.close();
} catch (Exception e) {
    e.printStackTrace();
}

valuesvideos.clear();
valuesvideos.put(MediaStore.Video.Media.IS_PENDING, 0);
mContext.getContentResolver().update(uriSavedVideo, valuesvideos, null, null);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...