尝试通过Gmail共享图片时,我收到了权限拒绝错误。
引起:java.lang.SecurityException:Permission Denial:从ProcessRecord打开提供程序android.support.v4.content.FileProvider {fd38d47 28029:com.google.android.gm/u0a65}(pid = 28029,uid = 10065 )不是从uid 10378中导出的
private void sendMail() {
Intent i = new Intent(Intent.ACTION_SENDTO);
i.setData(Uri.parse("mailto:"));
i.putExtra(Intent.EXTRA_EMAIL, new String[]{"xyz@gmail.com"});
i.putExtra(Intent.EXTRA_SUBJECT, "test");
File imagePath = new File(getFilesDir().getAbsolutePath(), "last_img");
File newFile = new File(imagePath, "lastimg.jpg");
Uri contentUri = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".provider", newFile);
i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
i.putExtra(Intent.EXTRA_STREAM, contentUri);
try {
startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}
}
提供商:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
provider_paths xml:
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="lastImage" path="last_img/" />
共享的图像实际上是在应用程序内创建的。
private void writeImage() {
String root = context.getFilesDir().getAbsolutePath();
File fileDir = new File(root + "/last_img");//no i18n
File file = new File(fileDir, "lastimg.jpg");//no i18n
if (!fileDir.exists()) {
fileDir.mkdirs();
}
if (file.exists()) {
file.delete();
}
file.createNewFile();
FileOutputStream fos = new FileOutputStream(file.getPath());
fos.write(data);
fos.close();
}
感谢任何帮助。
答案 0 :(得分:1)
最后我通过改变
解决了这个问题 i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
到
List<ResolveInfo> resInfoList = getPackageManager().queryIntentActivities(i, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfoList) {
String packageName = resolveInfo.activityInfo.packageName;
grantUriPermission(packageName, contentUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
提到here以解决问题。