32

I were trying to attach images from Drawable to an email (from my app to Gmail app)

I have tried the next code:

        Intent emailintent2 = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
        emailintent2.setType("image/*");
        emailintent2.putExtra(Intent.EXTRA_EMAIL, emailaddress2);
        emailintent2.putExtra(Intent.EXTRA_SUBJECT, CorAsunto);
        emailintent2.putExtra(Intent.EXTRA_TEXT, message2);

        ArrayList<Uri> uris = new ArrayList<Uri>();
        uris.add(Uri.parse("android.resource://" + getPackageName() + "/" + R.drawable.image1));
        uris.add(Uri.parse("android.resource://" + getPackageName() + "/" + R.drawable.image2));

        emailintent2.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
        startActivity(emailintent2);

But when I attach the image to the email I get the attach without the extension ".png" and thats is a big problem.

So I think in try to convert this Drawable images to Bitmap and also I think that the ArrayList will have to be Bitmap. I think that I will get the image has image defined in the attachment.

If it is possible, can someone tell me how to do it? Convert to Bitmap, add to Arraylist and attach the image.

If I am wrong in all what I said, can someone give me a solution? I need to attach the images from Drawable to the email with the extension (.png).

Roberto Zuniga
  • 437
  • 1
  • 7
  • 16

9 Answers9

59

There are 3 ways to perform conversion:

  1. Set the ImageView with resource image

    imageView.setImageResource(R.drawable.icon);
    

    and then get the bitmap from imageView

    Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();
    
  2. Get the drawable resource directly by Resource ID

    Bitmap icon = BitmapFactory.decodeResource(getResources(),R.drawable.profile_circle);
    
  3. Set the image on the ImageView then convert it into Bitmap (works for svg/VectorDrawable too)

    ImageView imgView = (ImageView) findViewById(R.id.ImageView);
    imgView.setImageResource(R.drawable.abc_image);
    z.setDrawingCacheEnabled(true);
    Bitmap bitmap = Bitmap.createBitmap(v.getDrawingCache());
    
Harish Gyanani
  • 1,366
  • 2
  • 22
  • 43
Droid_Mechanic
  • 1,474
  • 14
  • 13
34
Drawable myDrawable = getResources().getDrawable(R.drawable.anImage);
Bitmap anImage      = ((BitmapDrawable) myDrawable).getBitmap();

Also It can be defined in an XML file with the <bitmap> element.


2023 Approach

After 10 years from my first answer:

Drawable drawable = ContextCompat.getDrawable(context, R.drawable.your_drawable);

if (drawable instanceof BitmapDrawable) {
    // If your drawable is already a BitmapDrawable, you can get the bitmap directly
    Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
} else if (drawable instanceof VectorDrawable || drawable instanceof GradientDrawable) {
    // If it's another type of drawable (like VectorDrawable), you need to create a bitmap and draw the drawable onto it
    Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
                                        drawable.getIntrinsicHeight(),
                                        Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);
}
hakki
  • 6,181
  • 6
  • 62
  • 106
10

here is the piece of code , just check it out:

Bitmap Icon = BitmapFactory.decodeResource(getResources(), R.drawable.large_icon);
Nipun Gogia
  • 1,846
  • 1
  • 11
  • 17
6

The direct way is :

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);

You can configure the bitmap more if you define it in .xml drawable file as :

<?xml version="1.0" encoding="utf-8"?>
<bitmap
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@[package:]drawable/drawable_resource"
    android:antialias=["true" | "false"]
    android:dither=["true" | "false"]
    android:filter=["true" | "false"]
    android:gravity=["top" | "bottom" | "left" | "right" | "center_vertical" |
                      "fill_vertical" | "center_horizontal" | "fill_horizontal" |
                      "center" | "fill" | "clip_vertical" | "clip_horizontal"]
    android:mipMap=["true" | "false"]
    android:tileMode=["disabled" | "clamp" | "repeat" | "mirror"] />
Mahmoud
  • 2,683
  • 1
  • 30
  • 32
6
Bitmap icon = BitmapFactory.decodeResource(mContext.getResources(),
            R.drawable.ic_launcher);

Where mContext is your activity Context.

Rakesh
  • 2,732
  • 29
  • 28
2
public Bitmap convertToBitmap(Drawable drawable, int widthPixels, int heightPixels) {
    Bitmap mutableBitmap = Bitmap.createBitmap(widthPixels, heightPixels, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(mutableBitmap);
    drawable.setBounds(0, 0, widthPixels, heightPixels);
    drawable.draw(canvas);

    return mutableBitmap;
}

from https://msol.io/blog/android/android-convert-drawable-to-bitmap/ worked for me.

Alon
  • 883
  • 1
  • 6
  • 18
  • 1
    Thank you. This method allows get bitmap from vector drawable, whereas other only from bitmap drawable. – Andrey Nov 28 '20 at 17:03
1

METHOD 1 : Either you can directly convert to bitmap like this

 Bitmap myLogo = BitmapFactory.decodeResource(context.getResources(), R.drawable.my_drawable);

METHOD 2 : You can even convert the resource into the drawable and from that you can get bitmap like this

Bitmap myLogo = ((BitmapDrawable)getResources().getDrawable(R.drawable.logo)).getBitmap();

For API > 22 getDrawable method moved to the ResourcesCompat class so for that you do something like this

Bitmap myLogo = ((BitmapDrawable) ResourcesCompat.getDrawable(context.getResources(), R.drawable.logo, null)).getBitmap();
Sahil Bansal
  • 609
  • 8
  • 6
1
    public Bitmap getBitmap(@DrawableRes final int resId) {
        Drawable drawable = getResources().getDrawable(resId);;
        Canvas canvas = new Canvas();
        Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
                drawable.getIntrinsicHeight(),
                Bitmap.Config.ARGB_8888);
        canvas.setBitmap(bitmap);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        drawable.draw(canvas);
        return bitmap;
    }
-1

Simple as this

val bitmap = BitmapFactory.decodeResource(resources, R.drawable.some_image)

I got null for vector images but png image resources works fine.

ajw
  • 2,568
  • 23
  • 27