假设我有一个应用程序A (com.xxx.aaa),它的文件提供者来自拥有xml的getFilesDir()
<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, ...)上使用它。
发布于 2021-12-06 12:29:23
这种方法只在一个应用程序中工作吗?
FileProvider.getUriForFile()是为应用程序自己的FileProvider服务的。应用程序B没有FileProvider,更不用说为App配置的应用程序了。而且,App没有访问该文件的权限,因此它不能像您试图使用FLAG_GRANT_READ_URI_PERMISSION那样授予任何其他应用程序访问该文件的权限。
和我没有选择定义自定义键,这两个应用程序都理解并在intent.putExtra上使用它( key,.?)
这似乎是一种直截了当的做法。
https://stackoverflow.com/questions/70244476
复制相似问题