Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a Drawable image from resources to a Bitmap

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

like image 413
Roberto Zuniga Avatar asked Mar 06 '13 18:03

Roberto Zuniga


People also ask

How do I create a Bitmap from resources?

To create a bitmap from a resource, you use the BitmapFactory method decodeResource(): Bitmap bitmap = BitmapFactory. decodeResource(getResources(), R. drawable.

How do I turn an ImageView into a Bitmap?

Bitmap bm=((BitmapDrawable)imageView. getDrawable()). getBitmap();

How do I convert to Drawables?

This example demonstrates how do I convert Drawable to a Bitmap in Android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.


2 Answers

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()); 
like image 80
Droid_Mechanic Avatar answered Oct 20 '22 03:10

Droid_Mechanic


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.

like image 38
hakkikonu Avatar answered Oct 20 '22 04:10

hakkikonu