java.lang.RuntimeException trying to use recycled bitmap - android

I have this error when I run this code. It says java.lang.RuntimeException trying to use recycled bitmap. They say I should use Bitmap.recycle() but I'm having this error. Is my code correct?
Here is my code:
ImageView image = (ImageView) findViewById(R.id.imageView1);
Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
image.setImageBitmap(bMap);
bMap.recycle();

Check out this code:
if (null != bMap && !bMap .getBitmap().isRecycled())
{
bMap .getBitmap().recycle();
} else {
log("bitmap is already recycled");
}
I think it will help you.

Related

GetBitmap from ImageView

I know this question was asked a lot before but every time I try to retrieve the Bitmap from my ImageView it return null object
I tried :
ImageView imageView = findViewById(R.id.image);
Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
And
imageView.setDrawingCacheEnabled(true);
imageView.buildDrawingCache();
Bitmap bmap = imageView.getDrawingCache();
And also with glide.
Am I missing something or is there another way to achieve this ?
Thanks for help
Try this, hope it helps
ImageView imageView = findViewById(R.id.image);
BitmapDrawable bd = (BitmapDrawable) imageView.getDrawable();
Bitmap b = bd.getBitmap();
I have a kotlin solution.
val bitmap = binding.myImageView.getDrawable().toBitmap()
I hope that it has been useful.

How to get bitmap from NetworkImageView?

In my project I used (Volley + NetworkImageView) to download some images and texts and showing them in a list view.. till here, I don't have any problem.
Now, I want to get bitmaps form NetworkImageView and I tried many methods like the following, but non of them worked for me.
BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = drawable.getBitmap();
Another method:
imageView.buildDrawingCache();
Bitmap bmap = imageView.getDrawingCache();
Non of them worked..
Any Help is appreciated,,
You cannot get the Bitmap reference as it is never saved in the ImageView.
however you can get it using :
((BitmapDrawable)this.getDrawable()).getBitmap();
beacuse when you set it with Volley u do this:
/**
* Sets a Bitmap as the content of this ImageView.
*
* #param bm The bitmap to set
*/
#android.view.RemotableViewMethod
public void setImageBitmap(Bitmap bm) {
// Hacky fix to force setImageDrawable to do a full setImageDrawable
// instead of doing an object reference comparison
mDrawable = null;
if (mRecycleableBitmapDrawable == null) {
mRecycleableBitmapDrawable = new ImageViewBitmapDrawable(
mContext.getResources(), bm);
} else {
mRecycleableBitmapDrawable.setBitmap(bm);
}
setImageDrawable(mRecycleableBitmapDrawable);
}
however if you set your default image or error image or any other image in any other way you may not get BitmapDrawable but NinePatchDrawable for example.
here is how to check:
Drawable dd = image.getDrawable();
if(BitmapDrawable.class.isAssignableFrom(dd.getClass())) {
//good one
Bitmap bb = ((BitmapDrawable)dd).getBitmap();
} else {
//cannot get that one
}

Deletion of drawing cache

In my app I want to swap images at runtime when user clicks on it.
there are two imageviews when user click on first image and then click on second image at the same time I'm fetching bitmap of first imageview's image and assigning to second imageview for this I used following code:
public Bitmap createBitmap(ImageView imageview) {
imageview.setDrawingCacheEnabled(true);
imageview.buildDrawingCache(false);
if(imageview.getDrawingCache() != null) {
Bitmap bitmap = Bitmap.createBitmap(imageview.getDrawingCache());
imageview.setDrawingCacheEnabled(false);
return bitmap;
} else {
return null;
}
}
Code is working fine but the cache not cleared every time and the bitmap created with previous cache so how I can clear a bitmap cache?
This is a sample e.g. where i use to Free the native object associated with this bitmap.
Bitmap bitmap;
public Bitmap createBitmap(ImageView imageview) {
if (bitmap != null) {
bitmap.recycle();
bitmap = null;
}
bitmap = Bitmap.createBitmap(imageview.getDrawingCache());
// Your Code of bitmap Follows here
}
Before use of Bitmap just free the object.
use bitmap.recycle(); before valuating your bitmaps to clear their cache before recreating it.

ImageView not displaying Bitmap on real device

I get an Bitmap from internal storage or from the internet. When I try to display it in a ImageView, it works on different Emulator versions but not on my Galaxy S I9000. The Bitmap simply doesn't show up.
protected void onPostExecute (Bitmap bmp) {
progressBar.setVisibility(View.INVISIBLE);
if (bmp != null) {
imageView.setImageBitmap(bmp);
imageView.setVisibility(View.VISIBLE);
}
}
This is highly likely to be an underlying issue with openGL not accepting textures larger than 2400x2400. It doesn't cause crashes, but fails by not displaying the Bitmap in an ImageView.
You should always ensure that the Bitmap is smaller than 2400 in both dimensions and resize it before setting it on your view if it is larger.
I used this method to help me in displaying the image
public static Drawable ToDrawable(Resources r, byte[] encodedString)
{
if (encodedString == null)
return null;
Bitmap bmp = BitmapFactory.decodeByteArray(encodedString, 0, encodedString.length);
Drawable drawable = new BitmapDrawable(r, bmp);
// bmp.recycle();
return drawable;
}

How to get the whole bitmap attached to an ImageView?

I tried to get Bitmap attached to an ImageView, using ImageView.getDrawingCache(); But I found that the returned Bitmap was not the same as I'd like to get from the ImageView. It was always smaller than the real image.
I had known that, the method getDrawingCache() should not have the view if it is bigger than the screen as the visible portion of the view is only drawn and the cache holds only what is drawn.
Could I get the whole bitmap attached to a ImageView?
If you just want the Bitmap from a ImageView the following code may work for you:-
Bitmap bm=((BitmapDrawable)imageView.getDrawable()).getBitmap();
I think that's what you wanted.
If your drawble is not always an instanceof BitmapDrawable
Note: ImageView should be set before you do this.
Bitmap bitmap;
if (mImageView.getDrawable() instanceof BitmapDrawable) {
bitmap = ((BitmapDrawable) mImageView.getDrawable()).getBitmap();
} else {
Drawable d = mImageView.getDrawable();
bitmap = Bitmap.createBitmap(d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
d.draw(canvas);
}
Your bitmap is stored in bitmap.
Voila!
Easiest way is to set tag in ImageView.
imageView.setImageBitmap(bitmap);
imageView.setTag(bitmap);
To get Tag from it
imageView.getTag();

Categories

Resources