0

I am getting permission denial error while trying to share image through gmail.

Caused by: java.lang.SecurityException: Permission Denial: opening provider android.support.v4.content.FileProvider from ProcessRecord{fd38d47 28029:com.google.android.gm/u0a65} (pid=28029, uid=10065) that is not exported from 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:

   <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/" />

The image that is been shared is actually created inside the app.

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();
 }

Any help is appreciated.

1 Answers1

1

Finally i resolved it by changing

 i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

to

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);
}

Referred here to get this resolved.