问题描述
我在Android 11设备上获得了例外,但能够毫无问题地共享电子邮件中的这些文件。
Java:Lang:SecurityException: Permission Denial:Reading androidx.core.content.FileProvider.uri content://com.example.myapp.fileprovider/external_files_files/images/default_image.jpg from pid= 10847,uid=1000 requires the provider be exported or granUriPermission.
当我共享多个文件时,该问题仅适用于Android 11设备;否则当我使用Intent.setdata = uri(单个uri对象)时,单个文件可以正常工作而没有任何问题。
我的代码:
清单文件声明:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp">
<application
...>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.example.myapp.fileprovider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
</provider>
...
</application>
</manifest>
文件路径
<paths>
<external-files-path
name="images"
path="."/>
</paths>
共享代码。
ArrayList<Uri> Urilist = new ArrayList<String>();
// Adding multiple files as below.
File imagePath = new File(Context.getExternalFilesDir(null),"images");
File newFile = new File(imagePath,"default_image.jpg");
Uri contentUri = getUriForFile(getContext(),"com.example.myapp.fileprovider",newFile);
// Logic to add uri here
Intent shareIntent = new Intent();
shareIntent.setAction(Intent. ACTION_SEND_MULTIPLE);
shareIntent.putExtra(Intent.EXTRA_STREAM,Urilist);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
shareIntent.setType(*/*);
startActivity(Intent.createChooser(shareIntent,getResources().getText(R.string.share)));
谢谢。
解决方法
我通过替换下面的行来解决此问题。
startActivity(Intent.createChooser(shareIntent,getResources()。getText(R.string.share)));
使用
startActivity(shareIntent);