Android:发送带有来自 ImageView 的图像的电子邮件

标签 android android-imageview email-attachments

我是 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();
    }

}

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

最佳答案

试试这个

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

关于Android:发送带有来自 ImageView 的图像的电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11428189/

相关文章:

java - 如何显示sqlite数据库中的所有数据

android动画将圆形图像向各个方向扩展

php - 上传图片到服务器php-my-sql(android)

email - 使用 Microsoft graph 发送带有附件的电子邮件。微软代码示例不清楚

安卓矢量兼容性

java - 将数据从 Activity 传递到 SupportMapFragment

Android Studio 不允许我使用 repeatOnLifecycle

java - 是否可以通过 GSON 将 ImageView 发送到另一个 Activity?

php - 强制 phpmailer 发送空邮件

c# - 在不同的 smtp 端口上发送带有附件的电子邮件