0

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,,

1 Answer 1

1

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
    }
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks so much for your answer, so you mean I can use something like this: Bitmap btmap=((BitmapDrawable)this.getDrawable()).getBitmap();
yes i have used it myself but better check the type also othrwise u can get a casttype exception
I got this error: FATAL EXCEPTION: main Process: com.Activity, PID: 24829 java.lang.ClassCastException: android.graphics.drawable.TransitionDrawable cannot be cast to android.graphics.drawable.BitmapDrawable at com.SecondActivity$callOnClick.onClick(SecondActivity.java:233)
Thank you so much, I had a problem with new permission style in Android 6 :) now every things working well..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.