Android:发送包含ImageView图像的电子邮件

Ste*_*ano 1 android email-attachments android-imageview

我是stackoverflow的新手.我的Android应用程序有点问题,特别是使用ImageView触发事件.此事件打开一个带有一些预先写好的文本的电子邮件客户端,它应该附加图像的图像.我已经知道图像应该先转换成位图,然后压缩并发送到电子邮件客户端,但不幸的是我不是Android/Java专家,所以我找不到如何做到这一点.这是电子邮件方法的代码:

下面的新代码

我必须替换"String imageURI = null;" 以及电子邮件需要的图像.谢谢你们!

编辑:

我设法编辑了我的代码,没有错误:

public void sendMail(ImageView image){
    Intent i = new Intent(Intent.ACTION_SEND);
    int imageURI = R.drawable.img1;

    i.setType("text/plain");
    i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"destinatario@globelife.biz"});
    i.putExtra(Intent.EXTRA_SUBJECT, "Oggetto");
    i.putExtra(Intent.EXTRA_TEXT   , "Globelife");
    i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    i.setType("image/jpeg");
    i.putExtra(Intent.EXTRA_STREAM, Uri.parse("android.resource://"+getPackageName()+"/"+imageURI));


    try {
        startActivity(Intent.createChooser(i, "Send mail..."));
    } catch (android.content.ActivityNotFoundException ex) {
        Toast.makeText(Test01Activity.this, "Non sono presenti app per l'invio di e-mails.", Toast.LENGTH_SHORT).show();
    }

}
Run Code Online (Sandbox Code Playgroud)

但我需要更改"int imageURI = R.drawable.img1;" to"int imageURI = ImageView.src;" 或类似的东西

Moh*_*eem 6

试试这个

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

然后

   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..."));
Run Code Online (Sandbox Code Playgroud)

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;
 }
Run Code Online (Sandbox Code Playgroud)