【问题标题】:Send Image in message body of email android在电子邮件android的消息正文中发送图像
【发布时间】:2015-11-27 11:25:36
【问题描述】:

我的活动有一个图像视图、带有用户输入电子邮件地址的编辑文本和一个发送按钮。在发送按钮活动中,我准备好了电子邮件、主题和收件人姓名,但我还想添加 imageview 包含的图像。

    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setData(Uri.parse("mailto:"));
    intent.setType("image/jpeg");
    intent.putExtra(Intent.EXTRA_EMAIL, receivers);
    intent.putExtra(Intent.EXTRA_SUBJECT, "My subject");
    intent.putExtra(Intent.EXTRA_TEXT, "hello wats up");
    intent.putExtra(Intent.EXTRA_STREAM, image i want to send);
    startActivity(intent); 

显然最后的 putExtra 行会给我一个错误,说明他们想要字符串,但我正在传递 imageview。请指导我如何在此电子邮件正文中包含我的图像视图。 (不是作为附件,而是在带有消息文本的消息正文中)。

非常感谢您。

【问题讨论】:

    标签: java android email android-intent


    【解决方案1】:
     Intent.EXTRA_STREAM, image i want to send);
    

    一旦您使用 EXTRA_STREAM,您就会添加一个附件。

    如果您想在电子邮件正文中包含您的图片,您应该发送一封 html 邮件。不是纯文本邮件。

    【讨论】:

    • 我该怎么做?如果我找到 html 正文,我将无法找到正确的答案。你能发布它的 html 语法吗?
    • html 语法是正常的。抱歉,我从未在 Android 上发送过 html 邮件。谷歌为它。
    【解决方案2】:

    尝试如下:

    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.setType("image/*");
    Uri uri = Uri.parse("android.resource://your package name/"+R.drawable.ic_launcher);
    shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
    shareIntent.putExtra(android.content.Intent.EXTRA_EMAIL,recipients);
    shareIntent.putExtra(Intent.EXTRA_TEXT, message);
    startActivity(Intent.createChooser(shareIntent, "Send your image"));
    

    已编辑:

    如下声明文件变量

             File pic;
    

    在您的 OnActivityResult() 中应用如下更改:

    Bundle ext = data.getExtras();
        bmpEmail = (Bitmap)ext.get("data");
        try {
            File root = Environment.getExternalStorageDirectory();
            if (root.canWrite()){
                 pic = new File(root, "pic.png");
                FileOutputStream out = new FileOutputStream(pic);
                bmpEmail.compress(CompressFormat.PNG, 100, out);
                out.flush();
                out.close();
            }
        } catch (IOException e) {
            Log.e("BROKEN", "Could not write file " + e.getMessage());
        }   
    

    并在您的发送电子邮件代码中添加该行

          emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(pic));
    

    【讨论】:

      【解决方案3】:

      试试这个

      ImageView iv = (ImageView) findViewById(R.id.splashImageView);
      Drawable d =iv.getBackground();
      BitmapDrawable bitDw = ((BitmapDrawable) d);
      Bitmap bitmap = bitDw.getBitmap();
      File  mFile = savebitmap(bitmap);
      

      然后

      Uri u = null;
         u = Uri.fromFile(mFile);
      
         Intent emailIntent = new Intent(Intent.ACTION_SEND);
         emailIntent.setType("image/*");
         emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Hello...");
         // + "\n\r" + "\n\r" +
         // feed.get(Selectedposition).DETAIL_OBJECT.IMG_URL
         emailIntent.putExtra(Intent.EXTRA_TEXT, "Your tsxt here");
         emailIntent.putExtra(Intent.EXTRA_STREAM, u);
         startActivity(Intent.createChooser(emailIntent, "Send email..."));
      

      和savebitmap方法

         private File savebitmap(Bitmap bmp) {
        String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
        OutputStream outStream = null;
        File file = new File(extStorageDirectory, temp + ".png");
        if (file.exists()) {
         file.delete();
         file = new File(extStorageDirectory, temp + ".png");
        }
      
        try {
         outStream = new FileOutputStream(file);
         bmp.compress(Bitmap.CompressFormat.PNG, 100, outStream);
         outStream.flush();
         outStream.close();
        } catch (Exception e) {
         e.printStackTrace();
         return null;
        }
        return file;
       }
      

      【讨论】:

        【解决方案4】:

        您必须将 fileUri 作为第二个参数传递。像这样。

        intent.putExtra(Intent.EXTRA_STREAM, fileUri);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-09-10
          • 2012-11-01
          • 2018-05-05
          相关资源
          最近更新 更多