【问题标题】:Sharing through email only in android using Intent使用 Intent 仅在 android 中通过电子邮件共享
【发布时间】:2014-05-20 16:32:38
【问题描述】:

我想使用 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);
}

【问题讨论】:

  • @SanketKachhela 它显示了许多选项,例如 Skype、Gmail、Office Suite wifi Direct。我只想在 gmail 上分享。
  • @user2695306 所以......你必须在你的清单文件中给出一个特定的意图操作......
  • @PiyushGupta 请帮助我怎么能这样
  • 两点... 1. 您使用的是startActivityForResult(...) - 我可能错了,但我不确定GMail 应用程序是否真的会返回结果。 2. 然而,重点是您使用的是Intent.createChooser,它(顾名思义)会自动创建一个已安装应用程序列表,这些应用程序已注册以对特定 MIME 类型执行特定操作。

标签: android android-intent


【解决方案1】:

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

试试这个

intent.setType("message/rfc822");

【讨论】:

  • 我不想只获得 gmail 选项。我想直接打开带有附件的gmail应用。
  • 您可以通过在 Gmail 中明确指定撰写活动来仅获取 Gmail,但除此之外,我同意 @SanketKachhela
  • (*专门使用类名是愚蠢的,因为并非每个用户都会拥有 Gmail,而且撰写活动的类名可能会因版本而异)
  • @ataulm 我如何在意图中指定类名,以便它只提供选项 gmail。请帮忙
  • @user2695306 stackoverflow.com/questions/3470042/… AND stackoverflow.com/questions/13455118/… - 请注意,由于我之前评论中的原因(可能还有更多),它没有记录,而且很愚蠢
【解决方案2】:

试试这个:

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】:

    对我来说它的工作.. 试试 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;

    希望对你有帮助。

    【讨论】:

      【解决方案4】:

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

      或者你可以使用:

      startActivity(intent);
      

      【讨论】:

        【解决方案5】:
            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 应用程序。试试这个代码,它将显示所有可以发送电子邮件的应用程序。我确定它不会显示您手机中的所有共享应用程序。但它会显示一些其他可以处理任何共享意图的应用程序。就像谷歌云端硬盘一样。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-01-18
          • 1970-01-01
          • 2017-03-30
          • 1970-01-01
          • 1970-01-01
          • 2018-07-04
          • 1970-01-01
          相关资源
          最近更新 更多