【问题标题】:Android save video in gallery from its pathAndroid从其路径将视频保存在画廊中
【发布时间】:2021-01-05 14:14:33
【问题描述】:

我希望你们都做得很好。我一直在尝试将视频保存在我的画廊中。我有已经保存在隐藏文件夹中的视频路径。我只想将该视频保存在我的画廊中。这是我可能会犯错误的代码。如果你能解决它,我会很感激你。

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

通过这段代码,我遇到了这个 no content provider /storage/emulated/0/android/data/ 异常。由于我已经添加了图像提供程序和写入存储的权限,但我不知道视频需要什么提供程序或代码有问题?

【问题讨论】:

    标签: java android file file-handling


    【解决方案1】:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-23
      • 2014-01-18
      • 1970-01-01
      • 2021-10-24
      • 2016-10-12
      • 1970-01-01
      相关资源
      最近更新 更多