开发者

Send email with bitmap object as an attachment in Android?

开发者 https://www.devze.com 2023-02-06 01:43 出处:网络
I want to send the bitmap as an attachment in the mail. The image is not stored in SDCARD or anywhere in the device. The bitmap objec开发者_运维百科t gets created at runtime and that should be sent as

I want to send the bitmap as an attachment in the mail. The image is not stored in SDCARD or anywhere in the device. The bitmap objec开发者_运维百科t gets created at runtime and that should be sent as an attachment.


Then, you must save the Bitmap to the SDCard, and then attach it to the email (I guess, you know how to do so).

Why is it necessary to save it to the SDCard? That's because the email app will have to read the file that it's going to be attached; thus, you have to pass the path and filename to the email client. As any other app, the email client can only access files stored in its own private directory or the SDCard.


/* Return Drawable Object from Specified imageUrl In Web

 @imageUrl : image Url in Web

 */

try {
/// Getting image from Web
    InputStream is = (InputStream) new URL(imageUrl).getContent();
    // storing image from stream
    drawable = Drawable.createFromStream(is, "srcName");
    is.close();
    // converting drawable object to Bitmap to store in content providers of Media
    Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
    // Store image in Devise database to send image to mail
    String path = Images.Media.insertImage(getContentResolver(), bitmap,"title", null);
    Uri screenshotUri = Uri.parse(path);
    final Intent emailIntent1 = new Intent(     android.content.Intent.ACTION_SEND);
    emailIntent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    emailIntent1.putExtra(Intent.EXTRA_STREAM, screenshotUri);
    emailIntent1.setType("image/png");
    startActivity(Intent.createChooser(emailIntent1, "Send email using"));

}
catch(Exception e) { }
0

精彩评论

暂无评论...
验证码 换一张
取 消