【问题标题】:Setup FileProvider from getFilesDir() which can access by others app?从其他应用程序可以访问的 getFilesDir() 设置 FileProvider?
【发布时间】:2022-01-11 14:27:15
【问题描述】:

假设我有一个应用程序 A (com.xxx.aaa) 的文件提供程序来自 getFilesDir() 有xml

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

AndroidManifest:

<provider
     android:name="android.support.v4.content.FileProvider"
     android:authorities="com.xxx.aaa.fileprovider"
     android:exported="false"
     android:grantUriPermissions="true">
     <meta-data
          android:name="android.support.FILE_PROVIDER_PATHS"
          android:resource="@xml/fileprovider_paths"/>
</provider>

在其他应用 B (com.xxx.bbb) 想要求应用 A 对其来自getFilesDir() 的文件进行一些处理,假设应用 A 已经知道应用 B 文件名(target.txt)

try{
    Intent intent = new Intent("com.xxx.aaa.DO_SOMETHING_ON_TARGET");
    intent.setClassName("com.xxx.aaa","com.xxx.aaa.TargetActivity");
    File file = new File("/data/data/com.xxx.aaa/files/target.txt");
    Uri contentUri = FileProvider.getUriForFile(context, "com.xxx.aaa.fileprovider", file);
    intent.setData(contentUri);
    intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    Log.d(TAG, "setted fileprovider uri: "+contentUri);
    context.startActivity(intent);
}catch(Exception e){
    Log.e(TAG, "getUriForFile failed", e);
}

会输出异常:

IllegalArgumentException: Failed to find configured root /data/data/com.xxx.aaa/files/target.txt

这种方法是否只适用于一个应用程序?而且我别无选择定义两个应用程序都可以理解并在intent.putExtra(key, ...)上使用它的自定义键?

【问题讨论】:

    标签: android android-intent android-fileprovider android-internal-storage


    【解决方案1】:

    这种方法是否只适用于一个应用程序?

    FileProvider.getUriForFile() 是应用的自己的 FileProvider。应用 B 没有FileProvider,更不用说为应用 A 配置了一个。此外,应用 B 无权访问该文件,因此它无法授予任何其他应用访问该文件的权限,因为您正在尝试使用FLAG_GRANT_READ_URI_PERMISSION

    我没有选择定义两个应用程序都理解并在 intent.putExtra(key, ...) 上使用它的自定义键?

    这似乎是一种直截了当的方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-03
      • 1970-01-01
      • 1970-01-01
      • 2012-10-07
      • 2015-07-14
      • 2012-07-20
      • 2017-08-18
      相关资源
      最近更新 更多