仅使用Intent在Android中通过电子邮件共享

时间:2013-08-24 05:59:03

标签: android android-intent

我想通过使用Intent的电子邮件发送照片。我正在使用下面的代码,但它不仅仅打开gmail,但显示了许多共享选项。

请帮我分享一下唯一的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);
}

5 个答案:

答案 0 :(得分:2)

不能只获得gmail。但您可以定位某些内容类型应用程序。

试试这个

intent.setType("message/rfc822");

答案 1 :(得分:0)

对我来说它的工作.. 尝试Intent.ACTION_SENDTO

这是方法。

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

startActivityForResult将返回结果。如果邮件发送成功,则MESSAGE_RESULT是预期的结果。

上抓住结果
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();
        }
    }

}

在开头宣布static final int MESSAGE_RESULT = 1;

希望它有所帮助。

答案 2 :(得分:0)

试试这个:

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

答案 3 :(得分:0)

使用Intent.ACTION_VIEW代替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"));

或者您可以使用:

startActivity(intent);

答案 4 :(得分:-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) {
}

仅使用gmail应用程序并不是一个好主意。因为有很多手机没有安装gmail应用程序。试试这个代码,它会显示所有可以发送电子邮件的应用程序。我确定它不会显示手机中的所有共享应用。但它会显示其他一些可以处理任何共享意图的应用程序。像谷歌驱动器。