Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting Bitmap to Drawable show nothing

I am converting Bitmap into Drawable using following code.

MemoryCache memoryCache = new MemoryCache();
Bitmap bitmap = memoryCache.get(thumbnail);
Drawable drawable = (Drawable)new BitmapDrawable(bitmap); 

The MemoryCache I have used is from LazyList Project and it works fine when I use bitmap but when I convert it to drawable then it shows nothing in place of image.

Please Help

like image 753
Arun Badole Avatar asked Feb 04 '12 06:02

Arun Badole


People also ask

How to convert Bitmap to drawable in Android Studio?

Having seen a large amount of issues with bitmaps incorrectly scaling when converted to a BitmapDrawable , the general way to convert should be: Drawable d = new BitmapDrawable(getResources(), bitmap); Without the Resources reference , the bitmap may not render properly, even when scaled correctly.

How to use Bitmap drawable in android?

Android App Development for Beginners This example demonstrates how do I convert Bitmap to drawable 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.

What is BitmapDrawable?

android.graphics.drawable.BitmapDrawable. A Drawable that wraps a bitmap and can be tiled, stretched, or aligned. You can create a BitmapDrawable from a file path, an input stream, through XML inflation, or from a Bitmap object. It can be defined in an XML file with the <bitmap> element.

How to get drawable image id in android?

drawable id in the view's id: use v. setId() . Then get it back with v. getId() .


2 Answers

Change this line:

Drawable drawable = (Drawable)new BitmapDrawable(bitmap);

To:

Drawable drawable = new BitmapDrawable(getResources(), bitmap);

Note that the BitmapDrawable(Bitmap bitmap) constructor has been deprecated (source) and using the above call apparently ensures it gets set correctly with the right target density.

like image 164
Marvin Pinto Avatar answered Oct 12 '22 23:10

Marvin Pinto


Try using the following

Drawable d = new BitmapDrawable(bitmap);
like image 42
ranjit patel Avatar answered Oct 12 '22 22:10

ranjit patel