【问题标题】:Android FileProvider: Can't use ContentResolver to fetch file?Android FileProvider:不能使用 ContentResolver 来获取文件?
【发布时间】:2016-04-29 16:04:02
【问题描述】:

我想从外部应用程序访问 FileProvider 内部存储上的文件:

InputStream inputStream=contentResolver.openInputStream(Uri.parse("content://com.example.android.fileprovider/path/to/file.txt"));

android:authorities="com.example.android.fileprovider" 是我在其他 (FileProvider) 应用的清单 xml 中指定的权限字符串。

还有:

<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <files-path name="path" path="path/"/>
</paths>

文件存在于(files)/path/to/file.txt下的内部FP的存储中

但我总是得到 IllegalArgumentException no root 等。这不是默认的 Android FileProvider 的使用方式吗?我误解了它的用途吗?例如为什么默认在exported=false 中?

【问题讨论】:

  • “但我总是得到 IllegalArgumentException no root 等。” -- 请发布一个实际的堆栈跟踪。 “例如,为什么默认情况下在exported=false 中?” -- 您应该通过应用程序与提供商一起获得Uri,然后它还会授予您对该Uri 内容的权限,例如通过FLAG_GRANT_READ_URI_PERMISSION
  • 为什么要从提供者那里获取 Uri?我只希望 ContentProvider 以静默方式处理来自设备上其他应用的请求。
  • "我为什么要从提供者那里获取 Uri?" -- 因为提供者没有被导出,所以带有提供者的应用程序需要授予你对你打算使用的每个Uri 的临时权限。 FileProvider 用于允许第三方应用处理应用内容上的ACTION_VIEW 等情况。

标签: android android-fileprovider


【解决方案1】:
  • 首先你必须将你的包名替换为 android:authorities="com.example.android.fileprovider"
  • 其次,不存在名称为“路径”的目录,它必须是 文件段中的图像/、视频/等内容

【讨论】:

    猜你喜欢
    • 2014-07-31
    • 1970-01-01
    • 2017-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-11
    • 2012-10-27
    相关资源
    最近更新 更多