如何在SDK版本33中将视频从外部存储复制到内部存储?

How to copy video from external storage to internal storage in SDK version 33?

提问人:Maor Cohen 提问时间:5/3/2023 最后编辑:Maor Cohen 更新时间:5/3/2023 访问量:81

问:

我有一个视频选取器,当用户选取视频时,我想将其复制到内部文件夹中并对其进行修改。可能吗?如何?

如果没有,我可以在哪里存储视频,以便用户无法访问它(因为我将修改它,我不希望他能够访问修改后的视频)

由以下代码处理的视频选取器:

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("video/*");
    String[] mimeTypes = {"video/mp4", "video/3gp", "video/mpeg"};
    intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
    activity.startActivityForResult(intent, REQUEST_CODE_VIDEO_PICKER);

然后,其结果:

Uri videoUri = data.getData();

我正在尝试使用以下方法复制文件:

uriSrc = "content://com.android.providers.media.documents/document/video%3A1000099794"

destPath = getFilesDir() + "/video.mp4";

public static boolean copyFileToInternalStorage(Context context, Uri uriSrc, String destPath) {
    try {
        //InputStream inputStream = context.getContentResolver().openInputStream(uriSrc);
        //File tempFile = File.createTempFile("video", ".mp4", context.getCacheDir());
        //FileOutputStream outputStream = new FileOutputStream(tempFile);

        //byte[] buffer = new byte[1024];
        //int read;
        //while ((read = inputStream.read(buffer)) != -1) {
        //    outputStream.write(buffer, 0, read);
        //}
        //outputStream.close();

        InputStream fileInputStream= context.getContentResolver().openInputStream(uriSrc);

        FileOutputStream outputStreamInternal = new FileOutputStream(destPath);
        byte[] buffer2 = new byte[1024];
        int length;
        while ((length = fileInputStream.read(buffer2)) > 0) {
            outputStreamInternal.write(buffer2, 0, length);
        }
        fileInputStream.close();
        outputStreamInternal.close();
    }
    catch (Exception e) {
        Log.d("file_log", "exception when copying the src file to the internal storage: " + e.getMessage());
    }
    return new File(destPath).exists();
}

它将视频复制到内部,但没有音频。

android android-contentprovider 文件输入流 android-internal-storage

评论

1赞 blackapps 5/3/2023
a video picker什么?请出示验证码。并告诉用户在选择文件时会得到什么。
1赞 blackapps 5/3/2023
紧接在第一个 while 块 an 丢失之后。然后你已经准备好了,因为你在缓存存储中有一个副本。outputStream.close()
1赞 blackapps 5/3/2023
FileInputStream fileInputStream = new FileInputStream(tempFile);这和下面的所有代码都没有意义。复制一个文件,然后复制复制的文件。为什么?
1赞 blackapps 5/3/2023
嗯。。非常令人困惑。它又是不同的。你做两份。不需要。只需从 Uri uriSrc 复制到 String destPath。删除临时文件内容。
1赞 blackapps 5/3/2023
显示你调用 copyFileToInternalStorage() 的 destPath 。

答: 暂无答案