How to save video to gallery using mediaStore.Video android?

571 Views Asked by At
public String getVideoFilePath() {
        return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES).getAbsolutePath() + "/" + System.currentTimeMillis() + "compress.mp4";
    }


    public static void exportMp4ToGallery(Context context, String filePath) {
        File file = new File(filePath);
        MediaScannerConnection.scanFile(context, new String[]{file.toString()}, null, null);
    }

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="29"/>
       android:requestLegacyExternalStorage="true"

Currently, I'm using the above approach. getVideoFilePath() will return a path to the compressor class after compression is successful it will call exportMp4ToGallery(Context context, String filePath).

Is it a good approach? or Should I use MediaStore.video. But I want to know how to do the same with mediaStore.Video. Please help if you know, please provide a code snnipet.

EDIT ANOTHER APPROACH IS:

        private String getFileName() {
        String path = getExternalFilesDir("TrimmedVideo").getPath();
        Calendar calender = Calendar.getInstance();
        String fileDateTime = calender.get(Calendar.YEAR) + "_" +
                calender.get(Calendar.MONTH) + "_" +
                calender.get(Calendar.DAY_OF_MONTH) + "_" +
                calender.get(Calendar.HOUR_OF_DAY) + "_" +
                calender.get(Calendar.MINUTE) + "_" +
                calender.get(Calendar.SECOND);
        String fName = "trimmed_video_";

        File newFile = new File(path + File.separator +
                (fName) + fileDateTime + "." + TrimmerUtils.getFileExtension(this, uri));
        return String.valueOf(newFile);
    }

This above code will give the compressor class a path to save it in app external directory and then copy that file to gallery using MediaStore like below:

    public Uri addVideo(String videoFilePath) {
        Uri uriSavedVideo;
        File createdvideo = null;
        ContentResolver resolver = getContentResolver();
        String videoFileName = "video_" + System.currentTimeMillis() + ".mp4";
        ContentValues valuesvideos;
        valuesvideos = new ContentValues();

        if (Build.VERSION.SDK_INT >= 29) {
            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);
            Uri collection = MediaStore.Video.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);
            uriSavedVideo = resolver.insert(collection, valuesvideos);
        } else {
            String directory  = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + Environment.DIRECTORY_MOVIES + "/" + "YourFolder";
            createdvideo = new File(directory, videoFileName);
            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.DATA, createdvideo.getAbsolutePath());
            uriSavedVideo = getContentResolver().insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, valuesvideos);
        }

        if (Build.VERSION.SDK_INT >= 29) {
            valuesvideos.put(MediaStore.Video.Media.DATE_TAKEN, System.currentTimeMillis());
            valuesvideos.put(MediaStore.Video.Media.IS_PENDING, 1);
        }

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

            FileOutputStream out = new FileOutputStream(pfd.getFileDescriptor());

            File videoFile = new File(videoFilePath);
            FileInputStream in = new FileInputStream(videoFile);

            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();
        }

        if (Build.VERSION.SDK_INT >= 29) {
            valuesvideos.clear();
            valuesvideos.put(MediaStore.Video.Media.IS_PENDING, 0);
            getContentResolver().update(uriSavedVideo, valuesvideos, null, null);
        }
        return uriSavedVideo;
    }
0

There are 0 best solutions below