2

I tried to create a video file path as the follows :

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

public static String createVideoPath(Context context, String fileName) {
    File imageThumbsDirectory = context.getExternalFilesDir("FOLDER");
    if (imageThumbsDirectory != null) {
        if (!imageThumbsDirectory.exists()) {
            imageThumbsDirectory.mkdir();
        }
    }
    String appDir = context.getExternalFilesDir("FOLDER").getAbsolutePath();
    File file = new File(appDir, fileName);
    return file.getAbsolutePath();
}

I call the method above like this : String videoPath = createVideoPath(getApplicationContext(),fileName);

I use this library for edits :

EZFilter.input(mainBitmap).addFilter(null).enableRecord(videoPath, true, false).into(renderView);

After the edit finished, I try to save the final video to the gallery as follows :

private static Uri publicDirURI(Context context, String fileName, String dir) {
        ContentValues valuesVideos;
        valuesVideos = new ContentValues();
        valuesVideos.put(MediaStore.Video.Media.RELATIVE_PATH, dir);
        valuesVideos.put(MediaStore.Video.Media.TITLE, fileName);
        valuesVideos.put(MediaStore.Video.Media.DISPLAY_NAME, fileName);
        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 = context.getContentResolver();
        Uri collection = MediaStore.Video.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);
        Uri uriSavedVideo = resolver.insert(collection, valuesVideos);
        return saveFileToPublicMovies(context, valuesVideos, uriSavedVideo, fileName);
    }


private static Uri saveFileToPublicMovies(Context context, ContentValues contentValues, Uri uriSavedVideo, String fileName) {
    ParcelFileDescriptor pfd;
    try {
        pfd = context.getContentResolver().openFileDescriptor(uriSavedVideo, "w");
        FileOutputStream out = null;
        if (pfd != null) {
            out = new FileOutputStream(pfd.getFileDescriptor());
            File videoFile = new File(context.getExternalFilesDir("FOLDER"), fileName);
            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();
    }
    contentValues.clear();
    contentValues.put(MediaStore.Video.Media.IS_PENDING, 0);
    context.getContentResolver().update(uriSavedVideo, contentValues, null, null);
    return uriSavedVideo;
}

But I always get an empty saved video with 0.00b, I've tried the app with device running Android 9, but with getExternalStoragePublicDirectory, and everyting works just fine, so the issue is not related with the library.

Could anyone help me to solve this issue, I'm stuck on it almost 1 week, thank you

Edit :

 private static Uri saveFileToPublicMovies(Context context, ContentValues contentValues, Uri uriSavedVideo, String fileName) {
        ParcelFileDescriptor pfd;
        try {
            pfd = context.getContentResolver().openFileDescriptor(uriSavedVideo, "w");
            FileOutputStream out = null;
            if (pfd != null) {
                out = new FileOutputStream(pfd.getFileDescriptor());
                File videoFile = new File(context.getExternalFilesDir("AppName"), fileName);
                FileInputStream in = new FileInputStream(videoFile);
                byte[] buf = new byte[8192];
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
                out.flush();
                out.getFD().sync();
                out.close();
                in.close();
                pfd.close();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        contentValues.clear();
        contentValues.put(MediaStore.Video.Media.IS_PENDING, 0);
        context.getContentResolver().update(uriSavedVideo, contentValues, null, null);
    

0 Answers0