如何解决权限拒绝:与FileProvider Android 11手机共享文件时出现安全异常
我在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);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。