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
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.
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.
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.
drawable id in the view's id: use v. setId() . Then get it back with v. getId() .
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.
Try using the following
Drawable d = new BitmapDrawable(bitmap);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With