android将视频从其路径保存到gallery中

d5vmydt9  于 2021-06-26  发布在  Java
关注(0)|答案(1)|浏览(569)

我希望你们都做得好。我一直在努力保存我的画廊视频。我有一个视频路径,它已经保存在一个隐藏的文件夹。我只想把那个视频保存在我的图库里。这是我可能出错的代码。如果你能解决这个问题,我会感谢你的。

File newfile;
            AssetFileDescriptor videoAsset = getContentResolver().openAssetFileDescriptor(Uri.parse( path), "r");
            FileInputStream in = videoAsset.createInputStream();
            String root = Environment.getExternalStorageDirectory().getAbsolutePath();
            File dir = new File(root + "/" + "Pictures");
            if (!dir.exists()) {
                dir.mkdirs();
            }
            newfile = new File(dir, "status_"+System.currentTimeMillis()+".mp4");
            if (newfile.exists()) newfile.delete();
            OutputStream out = new FileOutputStream(newfile);
            // Copy the bits from instream to outstream
            byte[] buf = new byte[1024];
            int len;

            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }

            in.close();
            out.close();

通过这段代码,我得到了这个无内容提供者/存储/模拟的/0/android/data/exception。因为我已经添加了图像和权限的供应商写存储,但我不知道什么供应商需要视频的或问题是与代码?

hxzsmxv2

hxzsmxv21#

我已经找到了解决方案,我犯的错误是没有从一开始就获得正确的fileinputstream。把这个换掉

AssetFileDescriptor videoAsset = getContentResolver().openAssetFileDescriptor(Uri.parse( path), "r");
 FileInputStream in = videoAsset.createInputStream();

带着这个和你的好去处:d

FileInputStream in = new FileInputStream(new File(path));

相关问题