0

使用 mediaRecorder 和 Camera API 录制视频后,我正在尝试将视频保存到用户的画廊(通过保存到他们的外部电影、DCIM 目录或其他)。

不幸的是,我从文档或 StackOverflow 中找到的代码似乎创建了它们各自的 URI,而没有引用录制的视频或保存它的文件。具体来说,我尝试使用的代码是:

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

    ContentValues valuesvideos;
    valuesvideos = new ContentValues();
    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);
    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);

    ParcelFileDescriptor pfd;

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

        assert pfd != null;
        FileOutputStream out = new FileOutputStream(pfd.getFileDescriptor());

// Get the already saved video as fileinputstream from here
            File storageDir = new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES), "Folder");
            File imageFile = new File(storageDir, "testStorage");

            FileInputStream in = new FileInputStream(imageFile);


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

它周围的代码和答案似乎暗示通过访问 MediaStore.VOLUME_EXTERNAL_PRIMARY 解析器可以访问保存的视频文件,但我没有看到对它的明确引用。在录制视频之前,我将分配给 mediaRecorder 的文件定义为:

static File getMediaFile(Context c) {

    File mediaStorageDir = c.getExternalFilesDir(null);
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());

    return new File(mediaStorageDir.getPath() + File.separator + "VID_" + timeStamp + ".mp4");

}

停止视频后,我收到此错误(引用此行:)Uri uriSavedVideo = resolver.insert(collection, valuesvideos);

java.lang.UnsupportedOperationException: 未知 URI: content://media/external_primary/video/media at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java.167) ...

如何正确创建 URI 并参考保存的视频?用文件中的 URIUri.fromFile(f);代替 uriSavedVideo 也不起作用。

4

1 回答 1

0

要将特定视频文件保存到图库,我将文件作为参数传递给new FileInputStream.

其次,正如Android 文档所述:

只有您的应用可以查看该文件,直到您的应用将 IS_PENDING 的值更改回 0。

因此,按照用户@blackapps 的建议,我手动更改了该状态。修改后的代码如下:

private static void addToApi29Gallery(File file, Context context) {
    String videoFileName = "video_" + System.currentTimeMillis() + ".mp4";

    ContentValues valuesvideos = new ContentValues();
    valuesvideos.put(MediaStore.Video.Media.RELATIVE_PATH, "Movies/" + "Trickshott");
    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.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); //all video files on primary external storage
    Uri uriSavedVideo = resolver.insert(collection, valuesvideos);

    ParcelFileDescriptor pfd;

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

        assert pfd != null;
        FileOutputStream out = new FileOutputStream(pfd.getFileDescriptor());

        // Get the already saved video as fileinputstream from here.
        FileInputStream in = new FileInputStream(file);
        
        byte[] buf = new byte[8192];
        int len;
        
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        
        out.close();
        in.close();
        pfd.close();
        
        valuesvideos.clear(); 
        valuesvideos.put(MediaStore.Video.Media.IS_PENDING, 0); // Only your app can see the files until pending is turned into 0.

        context.getContentResolver().update(uriSavedVideo, valuesvideos, null, null);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
于 2020-06-23T11:19:59.317 回答