将视频插入图库 [Android Q]

2023-11-27

记录一个SurfeceView我正在使用第 3 方library,这个库需要一个路径,在我的例子中保存的输出(录制的视频)是保存的视频路径 :

mRenderPipeline = EZFilter.input(this.effectBmp)
                .addFilter(new Effects().getEffect(VideoMaker.this, i))
                .enableRecord(savedVideoPath, true, false)
                .into(mRenderView);

录制停止后,视频应保存为保存的视频路径作为一条路径,当我测试代码时,也就是说,当我打开图库应用程序时,我可以看到那里保存的视频,但是当我在Android Q上测试时,我看不到任何东西。

Since getExternalStoragePublicDirectory and getExternalStorageDirectory已弃用,我尝试使用getExternalFilesDir如下:

private void getPath() {
    String videoFileName = "video_" + System.currentTimeMillis() + ".mp4";
    fileName = videoFileName;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        File imageFile = null;
        File storageDir = new File(
            getExternalFilesDir(Environment.DIRECTORY_MOVIES), 
            "Folder");
        source = storageDir;
        boolean success = true;
        if (!storageDir.exists()) {
            success = storageDir.mkdirs();
        }
        if (success) {
            imageFile = new File(storageDir, videoFileName);
            savedVideoPath = imageFile.getAbsolutePath();
        }
    } 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();
        }
    }
}

录音停止后,我转到文件资源管理器应用程序 > Android > 数据 > com.packageName > 文件 > 电影 > 文件夹,我可以在那里看到所有保存的视频,但在图库中看不到它们。

我尝试使用Intent.ACTION_MEDIA_SCANNER_SCAN_FILE刷新画廊,但不幸的是不起作用。

我也尝试过MediaScannerConnection:

MediaScannerConnection.scanFile(
    context, 
    new String[]{savedVideoPath}, 
    new String[]{"video/mp4"}, 
    new MediaScannerConnection.MediaScannerConnectionClient() {

    public void onMediaScannerConnected() {
    }

    public void onScanCompleted(String s, Uri uri) {
    }
});
  • 谁能帮我解决这个问题?我坚持了将近2天

您必须更改库才能使其与 Android Q 兼容。如果您无法执行此操作,您可以将视频复制到媒体库,然后删除在getExternalFilesDir()。之后,您就获得了视频的 URI,并且可以使用该 URI 执行您想要的操作。

如果您已保存视频getExternalFilesDir()您可以在这里使用我的示例:您获得的媒体 URI 是“uriSavedVideo”。这只是一个例子。还应该在后台复制一个大视频。

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());
    // get the already saved video as fileinputstream
    // The Directory where your file is saved
    File storageDir = new File(
        getExternalFilesDir(Environment.DIRECTORY_MOVIES), 
        "Folder");
    //Directory and the name of your video file to copy
    File videoFile = new File(storageDir, "Myvideo"); 
    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);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将视频插入图库 [Android Q] 的相关文章

随机推荐