如何在Android 10上创建视频文件路径

时间:2020-06-13 11:22:35

标签: java android storage mediastore android-10.0

我尝试创建如下视频文件路径:

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

我这样调用上面的方法:String videoPath = createVideoPath(getApplicationContext(),fileName);

我使用这个library进行编辑:

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

编辑完成后,我尝试将最终视频保存如下:

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

但是我总是得到一个空的保存为0.00b的视频,我已经在运行Android 9的设备上尝试了该应用,但是使用getExternalStoragePublicDirectory,并且一切正常,因此问题与库无关

谁能帮助我解决这个问题,我坚持了将近1周,谢谢

编辑:

 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("VAPORGRAM"), 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 个答案:

没有答案