Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send email through Intent : SecurityException

Here is how I'm sending email through Gmail App.

        Intent emailIntent = new Intent(Intent.ACTION_SEND);
        emailIntent.setClassName("com.google.android.gm", "com.google.android.gm.ComposeActivityGmail");
        emailIntent.setType("text/html");
        emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Puzzle");
        emailIntent.putExtra(Intent.EXTRA_TEXT, someTextHere));
        emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(attachmentFile));
         try {
            startActivityForResult(emailIntent, SHARE_PUZZLE_REQUEST_CODE);
        } catch (ActivityNotFoundException e) {
            showToast("No application found on this device to perform share action");
        } catch (Exception e) {
            showToast(e.getMessage());
            e.printStackTrace();
        }

It's not starting the Gmail App but shows the following message.

java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.SEND typ=text/html cmp=com.google.android.gm/.ComposeActivityGmail (has extras) } from ProcessRecord{8293c64 26854:com.xxx.puzzleapp/u0a383} (pid=26854, uid=10383) not exported from uid 10083

There are few questions regarding this on SOF and most of them are suggested to use exported = true. But I cannot use this solution as I'm launching the activity of another app. Could you please guide me?

like image 666
Santhosh Avatar asked Nov 16 '16 08:11

Santhosh


People also ask

How do I send intent to another app?

Sending binary contentIntent shareIntent = new Intent(); shareIntent. setAction(Intent. ACTION_SEND);


1 Answers

Try this

        Intent emailIntent = new Intent(Intent.ACTION_SEND);
        emailIntent.setType("text/html");
        final PackageManager pm = this.getPackageManager();
        final List<ResolveInfo> matches = pm.queryIntentActivities(emailIntent, 0);
        String className = null;
        for (final ResolveInfo info : matches) {
            if (info.activityInfo.packageName.equals("com.google.android.gm")) {
                className = info.activityInfo.name;

                if(className != null && !className.isEmpty()){
                    break;
                }
            }
        }
        emailIntent.setClassName("com.google.android.gm", className);
like image 145
Rajasekhar Avatar answered Oct 18 '22 05:10

Rajasekhar