1

I want to send the photo by only email using Intent. I am using below code but its not opening only gmail but showing many share options.

Please help me to share the only gmail.

Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("image/jpeg"); // put here your mime type
List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(share, 0);
if(!resInfo.isEmpty()) {
    Intent targetedShare = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
    ArrayList<Uri> uris = new ArrayList<Uri>();
    for (ResolveInfo info : resInfo) {
        if(info.activityInfo.packageName.toLowerCase().contains("gmail") || info.activityInfo.name.toLowerCase().contains("gmail")) {
            targetedShare.setType("image/jpeg"); // put here your mime type

            targetedShare.putExtra(Intent.EXTRA_SUBJECT, "Amplimesh Photo");
            targetedShare.putExtra(Intent.EXTRA_TEXT,"Attached the Quote");

            //Fetching the Installed App and open the Gmail App.
            for(int index = 0; index < productList.size(); index++) {
                ByteArrayInputStream byteInputStream = new ByteArrayInputStream(productList.get(index).getOverlayBitmap());
                Bitmap overLayBitmap = BitmapFactory.decodeStream(byteInputStream);

                String fileName = SystemClock.currentThreadTimeMillis() + ".png";

                //Save the bitmap to cache.
                boolean isSaved = Helper.saveImageToExternalStorage(overLayBitmap, getApplicationContext(), fileName);
                if(isSaved)
                    uris.add(Uri.fromFile(new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/amplimesh/images/" + fileName)));
            }
        }
    }

    targetedShare.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
startActivityForResult(Intent.createChooser(targetedShare, "Sending multiple attachment"), 12345);
}
user2695306
  • 589
  • 2
  • 7
  • 9
  • follow this http://stackoverflow.com/a/18225100/942224 – Sanket Kachhela Aug 24 '13 at 06:05
  • @SanketKachhela It show many options like Skype, Gmail, Office Suite wifi Direct. I want to share only on gmail. – user2695306 Aug 24 '13 at 06:07
  • @user2695306 So... you have to give a specific intent action to in your manifest file.. – Piyush Aug 24 '13 at 06:11
  • @PiyushGupta Please help how could i that – user2695306 Aug 24 '13 at 06:13
  • Two points... 1. You are using `startActivityForResult(...)` - I may be wrong but I'm not sure the GMail app would actually return a result. 2. The main point, however, is you are using `Intent.createChooser` which (as the name implies) will automatically create a list of installed apps which are registered to perform the specific action on the specific mime type. – Squonk Aug 24 '13 at 06:42

5 Answers5

2

You can not get only gmail. but you can target some content type application.

try this

intent.setType("message/rfc822");
Sanket Kachhela
  • 10,861
  • 8
  • 50
  • 75
  • I dont want to get only gmail in option. I want direct open the gmail app with attachment. – user2695306 Aug 24 '13 at 06:26
  • You could get only Gmail by specifying the Compose Activity in Gmail explicitly, but apart from this, I agree with @SanketKachhela – ataulm Aug 24 '13 at 06:48
  • (*using the class name specifically is silly as not every user will have Gmail, and the compose Activity class name can change from version to version) – ataulm Aug 24 '13 at 06:58
  • @ataulm How can i specify the classname in intent so that it will give option gmail only. Please help – user2695306 Aug 24 '13 at 10:55
  • @user2695306 http://stackoverflow.com/questions/3470042/intent-uri-to-launch-gmail-app AND http://stackoverflow.com/questions/13455118/activitynotfoundexception-exception-on-jelly-bean-for-gmail-composer - note, it's undocumented, and silly due to the reasons in my previous comment (and maybe more) – ataulm Aug 24 '13 at 11:44
0

For me its working.. try Intent.ACTION_SENDTO

This is the method.

public void emailShare()
{
    Intent emailIntent = new Intent(android.content.Intent.ACTION_SENDTO);
    emailIntent.setType("image/jpeg");
    //File bitmapFile = new File(Environment.getExternalStorageDirectory()+"DCIM/Camera/img.jpg");
    //myUri = Uri.fromFile(bitmapFile);
    emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///mnt/sdcard/DCIM/Camera/img.jpg"));
    emailIntent.setData(Uri.parse("mailto:"));


    startActivityForResult(Intent.createChooser(emailIntent, "Complete action using:"),PICK_CONTACT_REQUEST);
}

Where startActivityForResult is to get the result back. and MESSAGE_RESULT is the result expected if mail sent successfully.

Catch the result on

public void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (requestCode == MESSAGE_RESULT) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK) {
            Toast.makeText(getApplicationContext(), "E-Mail sent successfully", Toast.LENGTH_LONG).show();
        }
    }

}

Declare static final int MESSAGE_RESULT = 1; at the begining.

Hope it helps.

Girish Thimmegowda
  • 2,109
  • 2
  • 14
  • 15
0

Try this:

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 
emailIntent.setType("image/jpeg");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] {""}); 
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, EMAIL_SUBJECT); 
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, EMAIL_BODY);
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+fileName));
startActivity(Intent.createChooser(emailIntent, "Sharing Options"));
0

use Intent.ACTION_VIEW insted of Intent.ACTION_SEND

Intent intent = new Intent(Intent.ACTION_VIEW);  
Uri data = Uri.parse("mailto:?subject=" + "Subject" + "&body=" + "Body" + "&to=" + "email@mail.com");  
intent.setData(data);  
startActivity(Intent.createChooser(intent, "Choose app"));

or you can use:

startActivity(intent);
a fair player
  • 11,530
  • 9
  • 46
  • 48
-2
    String shareImageLocation="Image file address";//Give file address here
Intent i = new Intent(Intent.ACTION_SEND_MULTIPLE);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL,
        new String[] { "someone@someone.com" });
i.putExtra(Intent.EXTRA_SUBJECT, "Send photos");
i.putExtra(Intent.EXTRA_STREAM, shareImageLocation);
String bugReportBody = description;
i.putExtra(Intent.EXTRA_TEXT, bugReportBody);
ArrayList<Uri> uris = new ArrayList<Uri>();
File fileIn = new File(shareImageLocation);
uris.add(Uri.fromFile(fileIn));
i.putExtra(Intent.EXTRA_STREAM, uris);
try {
    startActivityForResult(
            Intent.createChooser(i, "Complete action using"),
            SEND_EMAIL);
} catch (android.content.ActivityNotFoundException ex) {
}

Using only gmail app is not a good idea. Because there lot of phones which doesn't have gmail app installed. Try this code which will show all the app which can send email. I'm sure it won't show all the sharing app in your phone. But it will show some others apps which can handle any sharing intent. Like google drive.

Vishal Vijay
  • 2,518
  • 2
  • 23
  • 45