【问题标题】:Cannot read file using ContentResolver.openInputStream(Uri)无法使用 ContentResolver.openInputStream(Uri) 读取文件
【发布时间】:2017-10-13 21:06:59
【问题描述】:

对于手机内存中存在的文件,路径为/system/data/recf/pic.jpg,使用openInputStream()方法构造inputStream会抛出FileNotFoundException。

这就是我正在做的-

ContentResolver cr = context.getContentResolver();
InputStream inputStream = null;
Uri filename = Uri.parse("file:///system/data/recf/pic.jpg");
inputStream = cr.openInputStream(filename);

但它正在抛出 FileNotFoundException。我已检查该文件是否存在。卡了很久,求大神帮忙。谢谢。

【问题讨论】:

  • 您正在测试什么操作系统版本?检查 FileProvider 文档
  • 操作系统版本为android 6.0.1。它显示的错误消息是这样的 - java.io.FileNotFoundException: /system/data/recf/pic.jpg: open failed: ENOENT (No such file or directory)

标签: android uri inputstream android-contentresolver


【解决方案1】:

我可以看到您正在为路径解析硬编码字符串

Uri filename = Uri.parse("file:///system/data/recf/pic.jpg");

并且您正在使用三个正斜杠传递文件。这可能是问题所在。 首先,您需要确保路径正确。您可以为此使用文件对象来验证是否存在具有提供路径的文件。您可以使用以下代码进行检查。

File file  = new File("Your path");
if(file.exists()) {
   // if this is true that means file exists in actual then
   //make uri and get the stream as you are already doing
}

这个问题很可能属于路径。希望我的回答对你有帮助。

【讨论】:

    【解决方案2】:

    设备的内部存储(手机存储)在 Android 方面仍被视为外部存储。 “外部存储”可能是指可移动存储,例如 SD 卡或手机的不可移动内部存储。

    “内部存储”是指您的应用程序的私有空间,不与任何人(包括用户)共享。

    因此,需要读取和写入外部存储的权限。在 Android M+ 上,需要实时权限。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-26
      • 1970-01-01
      相关资源
      最近更新 更多