有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

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

            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/安卓/data/异常。因为我已经添加了图像的提供者和写入存储的权限,但我不知道视频需要什么提供者,或者代码有问题


共 (1) 个答案

  1. # 1 楼答案

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

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

    有了这个和你的好去处:D

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