Convert BitmapDrawable to Bitmap - android

In my project a bitmap gets filled with text and another bitmap, the method to add the text to the bitmap returns a BitmapDrawable.
I want to save this BitmapDrawable as a pdf file, so I can email the original bitmap, with text and other image(s) added.
For this, I'll need a regular Bitmap instead of a BitmapDrawable.
I can't seem to find any answers on this, because I don't have a reference to a drawable in a drawable folder, I just have a BitmapDrawable to work with.
Any ideas on this?

BitmapDrawables support a getter for the bitmap.
Bitmap bitmap = bitmapDrawable.getBitmap();

Related

LRUCache BitmapDrawable

I'm using an LruCache to cache a lot of small BitmapDrawables used throughout my app. The problem is that the sizes differ for the different places I use the images.
I set the bounds when I retrieve the drawable from the cache before setting it to the ImageView to have the correct size.
When I set the bounds, the drawable resizes on the other places as well.
How can I get around this issue without using drawable.getConstantState().newDrawable()? Creating a new drawable from the cached drawable is very slow when scrolling a listview.
The same images are used in DynamicDrawableSpans where I can't set the bounds on the span itself, only on the drawable directly.
Will it be wise to have the same drawable image cached for the different contexts in seperate Caches?
I got it to work correctly.
I changed the LruCache type from BitmapDrawable to Bitmap. This way the cache only stores the Bitmap image and not the drawable bounds as well.
When I want to set the bitmap I convert it to BitmapDrawable
Drawable d = new BitmapDrawable(getResources(), bitmap);
Then I set the bounds for the newly created drawable instance
d.setBounds(0, 0, sizex, sizey);
This way each instance of the Bitmap is a seperate Drawable with its own Bounds.

How to get the image bitmap of an ImageButton?

I have no problem doing:
currentImageButton.setImageBitmap(bitmap);
However, I have yet to be able to get the image bitmap back. I am trying to get the image bitmap BACK from the ImageButton and use it to set the background of my canvas.
currentImageButton.buildDrawingCache();
Drawable myDrawable = currentImageButton.getDrawable();
Bitmap anImage = ((BitmapDrawable) myDrawable).getBitmap();
canvas.drawBitmap(getResizedBitmap(anImage, 1000, 1000));
None of this seems to work though. I've even tried using myDrawable to set change the background of my view, just incase it was something wrong with canvas but that doesn't work either.
Bitmap bitmap = ((BitmapDrawable)imageBitmapInstance.getDrawable()).getBitmap();

Create a bitmap from drawable resource filepath

I would like to create an image chooser from my drawable resources. I think I have found an "easy" way to do this. I was going to create a listview that displays the drawables , and the user would be able to pick one. I would then pass back , the path to this drawable resource, the paths would be hard-coded in. I then would set a bitmap to this drawable path. I was trying to do a simple test of this with just hard coded values, (no listview yet) but I do not get a visible bitmap back. I think it "works" , but maybe the bitmap is to small or not set to visible?
bm2 = BitmapFactory.decodeFile("/gds2/res/drawable-hdpi/feel_like_a_sir.png");
foreground_imageview.setImageBitmap(bm2);
Try to load your drawable in this way
bm2 = BitmapFactory.decodeResource(getResources(), R.drawable.feel_like_a_sir);
foreground_imageview.setImageBitmap(bm2);

Create Bitmap from drawable

I would like to add an image (Bitmap) over another one. For that I am only using Bitmaps (fot the image I draw in and the image I add), but the Bitmap I am using is actually a resource in my drawable resource. So Is there a way to create a Bitmap that contains my drawable ?
Use decodeResource() of BitmapFactory to convert drawable resource into Bitmap as follows...
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.your_drawable);
Try this:
Bitmap bitmap= BitmapFactory.decodeResource(context.getResources(),
R.drawable.icon_name);

How to clean up a bitmap?

I'm creating a new empty transparent Bitmap and making some modifications to it.
Is there a way to clean up this (make transparent and empty again) without creating new instance of Bitmap?
UPDATE:
Thanks for your help.
I have found answer.
// Fills the bitmap's pixels with the specified Color.
bitmap.eraseColor(somecolor);
Try this:
myBitmap.eraseColor(android.graphics.Color.TRANSPARENT);
The constant int value of TRANSPARENT is 0x00000000.
Your Bitmap must be a mutable Bitmap.
when your bitmap like this
Bitmap bitmap=.......
then convert it into mutable bitmap by using below
Bitmap mutableBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
then
mutableBitmap.eraseColor(somecolor);

Categories

Resources