How to convert a Drawable image from resources to a Bitmap

93,238

Solution 1

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());
    

Solution 2

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.

Solution 3

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

Bitmap Icon = BitmapFactory.decodeResource(getResources(), R.drawable.large_icon);

Solution 4

Bitmap icon = BitmapFactory.decodeResource(mContext.getResources(),
            R.drawable.ic_launcher);

Where mContext is your activity Context.

Solution 5

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"] />
Share:
93,238
Roberto Zuniga
Author by

Roberto Zuniga

Updated on December 12, 2020

Comments

  • Roberto Zuniga
    Roberto Zuniga over 3 years

    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).

  • Developine
    Developine almost 8 years
  • M. Usman Khan
    M. Usman Khan over 7 years
    this should be more efficient than other solutions, but I see that getDrawable is deprecated
  • Pochmurnik
    Pochmurnik over 5 years
    This doesn't work. I get ClassCastException: android.graphics.drawable.ColorDrawable cannot be cast to android.graphics.drawable.BitmapDrawable
  • Andrey
    Andrey over 3 years
    Thank you. This method allows get bitmap from vector drawable, whereas other only from bitmap drawable.
  • Bink
    Bink over 3 years
    It is often helpful to add some commentary when you post code.