Unable to convert bitmap to Drawable

I will convert Bitmap to Drawable using the following code.

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

The MemoryCache I used is from the LazyList Project, and it works fine when I use a bitmap, but when I convert it to drawable, then nothing is displayed instead of the image.

Please, help

+5
source share
3 answers

Change this line:

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

To:

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

Please note that the constructor is BitmapDrawable(Bitmap bitmap)deprecated ( source ), and using the above call seems to guarantee that it will be set correctly with the correct density target.

+16
source

Try using the following

Drawable d = new BitmapDrawable(bitmap);
+3
source

Problem found

Actually the problem was

MemoryCache memoryCache = new MemoryCache();
Bitmap bitmap = memoryCache.get(thumbnail);
0
source

All Articles