尝试将Uri实例作为附件附加到电子邮件意图时,我收到了FileUriExposedException。
我使用FileProvider模式处理Camera Intent结果,其配置如下:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true"
>
<meta-data android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>
但是,当我尝试将结果Uri实例附加到Email意图时,我得到了意想不到的结果。见下文(非常感谢帮助!):
final Uri pAttachment = getParentActivity().takeScreenshot();
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/html");
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{pEmailTo});
intent.putExtra(Intent.EXTRA_SUBJECT, pSubject);
intent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(pBody));
intent.putExtra(Intent.EXTRA_STREAM, pAttachment);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
pContext.startActivity(intent);
堆栈跟踪:
02-01 16:22:59.266 13989-14105/com.compuscan.vas.mycreditcheckmobile I/com.company.myapp.utils.EmailUtility: sendEmail: Support Ticket - 7258107843888828116
02-01 16:22:59.325 13989-14105/com.company.myapp I/com.company.myapp.utils.EmailUtility: sendEmail - failed: file:///storage/emulated/0/Android/data/com.company.myapp/files/Pictures/screenshots_com.company.myapp/2018-02-01_16-21-52.031.png exposed beyond app through ClipData.Item.getUri()
02-01 16:22:59.329 13989-14105/com.company.myapp W/System.err: android.os.FileUriExposedException: file:///storage/emulated/0/Android/data/com.company.myapp/files/Pictures/screenshots_com.company.myapp/2018-02-01_16-21-52.031.png exposed beyond app through ClipData.Item.getUri()
02-01 16:22:59.330 13989-14105/com.company.myapp W/System.err: at android.os.StrictMode.onFileUriExposed(StrictMode.java:1814)
02-01 16:22:59.331 13989-14105/com.company.myapp W/System.err: at android.net.Uri.checkFileUriExposed(Uri.java:2389)
02-01 16:22:59.331 13989-14105/com.company.myapp W/System.err: at android.content.ClipData.prepareToLeaveProcess(ClipData.java:832)
02-01 16:22:59.331 13989-14105/com.company.myapp W/System.err: at android.content.Intent.prepareToLeaveProcess(Intent.java:8922)
02-01 16:22:59.333 13989-14105/com.company.myapp W/System.err: at android.content.Intent.prepareToLeaveProcess(Intent.java:8907)
02-01 16:22:59.334 13989-14105/com.company.myapp W/System.err: at android.app.Instrumentation.execStartActivity(Instrumentation.java:1517)
02-01 16:22:59.335 13989-14105/com.company.myapp W/System.err: at android.app.Activity.startActivityForResult(Activity.java:4247)
02-01 16:22:59.336 13989-14105/com.company.myapp W/System.err: at android.support.v4.app.BaseFragmentActivityApi16.startActivityForResult(BaseFragmentActivityApi16.java:54)
02-01 16:22:59.336 13989-14105/com.company.myapp W/System.err: at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:67)
02-01 16:22:59.337 13989-14105/com.company.myapp W/System.err: at android.app.Activity.startActivityForResult(Activity.java:4206)
02-01 16:22:59.338 13989-14105/com.company.myapp W/System.err: at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:732)
02-01 16:22:59.338 13989-14105/com.company.myapp W/System.err: at android.app.Activity.startActivity(Activity.java:4530)
02-01 16:22:59.339 13989-14105/com.company.myapp W/System.err: at android.app.Activity.startActivity(Activity.java:4498)