安卓:与图像从ImageView的发送电子邮件 [英] Android: send an email with an image from an ImageView
问题描述
我是新来的计算器。我有一个小问题,我的Android应用程序,expecially与触发事件在点击一个ImageView的。本次活动打开一个电子邮件客户端的一些pre-书面文本,并应附有图像的图像。我已经知道,形象应该是之前转换为位图,然后COM pressed并将其发送到电子邮件客户端,但不幸的是我不是一个Android / Java专家,所以我无法找到如何做到这一点。这是电子邮件的方法的code:
I'm new here on stackoverflow. I have a little problem with my Android app, expecially with an ImageView that triggers an event on tap. This event opens an email client with some pre-written text and it should attach the image of the Image. I already know that the image should be converted into a bitmap before, then compressed and send it to the email client, but unfortunatly I'm not an Android/Java expert so I can't find how to do that. This is the code of the email method:
新的$ C $低于C 的
在哪里我不得不更换字符串imageURI = NULL;什么电子邮件需要的图像。谢谢大家!
Where I have to replace "String imageURI = null;" with what the email needs as image. Thank you all!
编辑:
我设法修改我的code到这一点,即没有给出错误的:
I managed to edit my code to this, that gives no errors:
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();
}
}
不过,我需要改变诠释imageURI = R.drawable.img1;为int imageURI = ImageView.src;或者类似的东西。
But I need to change "int imageURI = R.drawable.img1;" to "int imageURI = ImageView.src;" or something like that
推荐答案
试试这个
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;
}
这篇关于安卓:与图像从ImageView的发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!