Android OutOfMemoryException while changing ImageView Resource - android

in my app i change the resource of an ImageView a lot of times with this method:
private void setIcon(int id, int imageSource) {
((ImageView)findViewById(id)).setBackgroundResource(imageSource);
}
After a while i get an OutOfMemoryException. Is there a better method to free the old resource (image) before i load the new one?
EDIT
private void setIcon(int id, int imageSource) {
ImageView imageView = (ImageView)findViewById(id);
Drawable bg= imageView.getBackground();
if(bg != null && bg instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) bg;
bitmapDrawable.getBitmap().recycle();
if(D)Log.d(TAG, "recycled bitmap");
}
Bitmap bm = BitmapFactory.decodeResource(getResources(), imageSource);
imageView.setImageBitmap(bm);
}

Before setting the new background, try to clear the memory for the old one, like this:
Drawable bg = image.getBackground();
if(bg != null && bg instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) bg;
bitmapDrawable.getBitmap().recycle();
}
Do you have specific reasons to set the backgroundResource? Can't you simply use image.setResource(). That way it is easier to obtain the bitmap and recycle it.
Edit: if you only want one image (no overlays or something) than this is easier (and how it's supposed to be):
ImageView image = (ImageView)findViewById(id);
image.setImageResource(imageSource);
Using bitmaps:
Bitmap bm = BitmapFactory.decodeResource(getResources(), id);
image.setImageBitmap(bm);
image.getDrawable(); // will return a BitmapDrawable

Related

Using bitmap to take image from imageview

I'm having a problem using bitmap to take image from imageview... The code for using bitmap to take image by its ID is: (my picture name = test_image)(my imageview = imageView)
Bitmap image;
...
image = BitmapFactory.decodeResource(getResources(), R. drawable.test_image);
But I want it to take the image inside the imageview.Without specifying the image name.
Due to the reason that I want to upload image into the imageview and process the image that is in imageview.
BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = drawable.getBitmap();
This is how you can get bitmap from any imageview
private Bitmap getImageViewFromBitmap(ImageView v){
Bitmap bm=((BitmapDrawable)v.getDrawable()).getBitmap();
return bm;
}
private Bitmap drawableToBitmap (Drawable drawable) {
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable)drawable).getBitmap();
}
int width = drawable.getIntrinsicWidth();
width = width > 0 ? width : 1;
int height = drawable.getIntrinsicHeight();
height = height > 0 ? height : 1;
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
Call this function as below :
Drawable drawable = imageView.getDrawable();
Bitmap bitmap = drawableToBitmap(drawable);

BitmapDrawable (how to get background image and compare to an image in drawable folder.)

I want get an image background on a textview and compare it in an if statement..and do some actions.
this my code.
BitmapDrawable cd = (BitmapDrawable) numberseven.getBackground();
BitmapDrawable cdeight = (BitmapDrawable) numbereight.getBackground();
BitmapDrawable cdnine = (BitmapDrawable) numbernine.getBackground();
BitmapDrawable cdtwelve = (BitmapDrawable) numbertwelve.getBackground();
BitmapDrawable cdthirteen = (BitmapDrawable) numberthirteen.getBackground();
BitmapDrawable cdfourteen = (BitmapDrawable) numberfourteen.getBackground();
BitmapDrawable cdseventeen = (BitmapDrawable) numberseventeen.getBackground();
BitmapDrawable cdeighteen = (BitmapDrawable) numbereighteen.getBackground();
BitmapDraw`enter code here`able cdnineteen = (BitmapDrawable) numbernineteen.getBackground();
if(cd.getBitmap()==R.drawable.green){
}
please someone asist.
You can convert your drawable resource to bitmap and compare them
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.green);
if(cd.getBitmap().equals(bitmap)){
// same
}

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
}

How to Convert a Transition Drawable to Drawable or to Bitmap

I'm looking for a way on how can I convert a TransitionDrawable to Drawable or To bitmap to be able to save the Image.
I have been looking for a while and I tried a lot methods and no clue.
here a code what I have tried :
TransitionDrawable drawable = (TransitionDrawable) profil.getDrawable();
Drawable drawalb = drawable.mutate();
final Bitmap bitmap = ((BitmapDrawable) drawalb).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] image = stream.toByteArray();
I wantn't able to do that, but I have found another clue, I was able to get the bitmap directly before converting it to TransitionDrawable.
A transitionDrawable is an array of drawable (Defnition).
so;
1.Get the drawable you want, depending on its index
2.covert it to a bitmap (As cited here by Andre)
TransitionDrawable Tdrawable =(TransitionDrawable) imageView.getDrawable();
Drawable mDrawable = Tdrawable.getDrawable;
bitmap = drawableToBitmap(mDrawable);
public static Bitmap drawableToBitmap (Drawable drawable) {
Bitmap bitmap = null;
if (drawable instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
if(bitmapDrawable.getBitmap() != null) {
return bitmapDrawable.getBitmap();
}
}
if(drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel
} else {
bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
}
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}

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;
}

Categories

Resources