0
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;
    }
ThinQ
  • 19
  • 5
  • Maybe for you help that codes; [link](https://stackoverflow.com/a/63725349/11577746) – ERTUGRUL KOC Dec 01 '22 at 12:17
  • That approach is ok. And further i have no idea what you mean with mediaStore.Video. – blackapps Dec 01 '22 at 13:42
  • @blackapps will it work on all API levels? and is it okay to put it in the Google play store? – ThinQ Dec 01 '22 at 15:52
  • @blackapps please check my other approach. which is the best approach. My app's minimum API is 23 and the max is 33. Note: I know about the android 13 image & video permissions. – ThinQ Dec 01 '22 at 15:58
  • `ANOTHER APPROACH IS: private String getFileName() { ` ??? getFileName() just gives you a file name. I do not understand that you consider that another approach. – blackapps Dec 01 '22 at 16:26
  • `addVideo()` will make a copy of a file. Do you want two files on your device? – blackapps Dec 01 '22 at 16:27
  • @blackapps ok I want something like first I want to download a video then I want to add my app logo to that video using media codec then save that video to the galley – ThinQ Dec 01 '22 at 16:31
  • Make a new post for that. As it is quite something different. – blackapps Dec 01 '22 at 16:32
  • @blackapps I need your help, please. I know you are a storage expert. my discord: asit#2561 – ThinQ Dec 01 '22 at 16:33
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/250053/discussion-between-thinq-and-blackapps). – ThinQ Dec 01 '22 at 16:34

0 Answers0