Java 在电子邮件的邮件正文中发送图像

Java 在电子邮件的邮件正文中发送图像,java,android,email,android-intent,Java,Android,Email,Android Intent,“我的活动”有一个imageview、带有用户输入电子邮件地址的edittext和一个send按钮。在发送按钮活动中,我已准备好电子邮件、主题和收件人姓名,但我还想添加imageview包含的图像 Intent intent = new Intent(Intent.ACTION_SEND); intent.setData(Uri.parse("mailto:")); intent.setType("image/jpeg"); intent.putExtra(Int

“我的活动”有一个imageview、带有用户输入电子邮件地址的edittext和一个send按钮。在发送按钮活动中,我已准备好电子邮件、主题和收件人姓名,但我还想添加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); 
显然,最后一行会给我错误,说明他们想要字符串,但我正在传递imageview。请指导我如何将imageview包含在此电子邮件正文中。(不是作为附件,而是在带有消息文本的消息正文中)


很多人提前感谢您。

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

intent.putExtra(Intent.EXTRA_STREAM, fileUri);
尝试如下:

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"));
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());
    }   
编辑:

声明文件变量,如下所示

         File pic;
在OnActivityResult()中,按如下方式应用更改:

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"));
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));
试试这个

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..."));
和保存位图方法

   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;
 }
一旦您使用额外的_流,您就添加了一个附件


如果你想在邮件正文中显示你的图像,你应该发送一封html邮件。不是纯文本邮件。

我该怎么做?如果我发现有html正文,我就找不到合适的ans。你能发布html语法吗?html语法和正常语法一样。很抱歉,我从未在Android上发送过html邮件。谷歌就是这样。