0

I'm using ION (https://github.com/koush/ion) to load images from the web into a ListView of ImageView. Currently I want to get the bitmap from the ImageView, but I'm getting one exception that is force closing my app:

java.lang.ClassCastException: com.koushikdutta.ion.IonDrawable cannot be cast to android.graphics.drawable.BitmapDrawable

These are the lines that I'm using:

final ImageView itemImageView = (ImageView)view.findViewById(R.id.photoImage);
final Bitmap itemDrawable = ((BitmapDrawable) itemImageView.getDrawable()).getBitmap();

How can I solve this? Thanks in advance!

Tripp Kinetics
  • 5,178
  • 2
  • 23
  • 37
pluralism
  • 1,487
  • 2
  • 16
  • 22
  • What are you trying to do in this line. `final Bitmap itemDrawable = ((BitmapDrawable) itemImageView.getDrawable()).getBitmap();` – Shivam Verma Jul 02 '14 at 16:02
  • @ShivamVerma I'm trying to get the bitmap of the ImageView, but BitmapDrawable can't be casted to IonDrawable. – pluralism Jul 02 '14 at 16:03
  • what exactly do you want to achieve. This might not be the best method of doing what you're trying to do. – Shivam Verma Jul 02 '14 at 16:04
  • 1
    Have you consider to use volley's imageloader? I found it really easy to use and you can cast it to a bitmap with no problem. Check [this](http://www.androidhive.info/2014/05/android-working-with-volley-library-1/) and [this](http://blog.lemberg.co.uk/volley-part-3-image-loader) tutorials if you are interested – Carlos J Jul 02 '14 at 16:04
  • Ion sets a custom drawable, so it can't be cast to a BitmapDrawable. Use: Ion.with(imageView).getBitmap() to get the bitmap out – koush Dec 26 '14 at 08:41

4 Answers4

5

Ion sets a custom drawable to manage animated GIFs, fade transitions, etc, so it can't be cast to a BitmapDrawable.

Use:

Ion.with(imageView).getBitmap()

to get the bitmap out

koush
  • 2,972
  • 28
  • 31
3

If you set a callback when making the Ion request and have it return an ImageViewBitmapInfo you can retrieve bitmaps that way. For e.g.

Ion.with(imageView)
   .placeholder(R.drawable.default_avatar)
   .load(userImageURL)
   .withBitmapInfo()
   .setCallback(new FutureCallback<ImageViewBitmapInfo>() {
       @Override
       public void onCompleted(Exception e, ImageViewBitmapInfo result) {
           // Check for exceptions

           // Check bitmaps first
           Bitmap b = result.getBitmapInfo().bitmaps[0];
           ...
       };
kingdonnaz
  • 101
  • 5
1

It may be worth looking into:

nostra13's "Universal Image Loader"

It's an awesome library with tons of features to display images from URLs, cast to bitmaps, etc.

Jonny07
  • 625
  • 3
  • 14
  • Thank you! This solved my problem! Took 5min to replace everything! – pluralism Jul 02 '14 at 16:17
  • @Jonny07 : Can you let me know is it possible to convert and XML (has an imageView) to Bitmap in UniversalImageLoader? I am trying out the following: http://goo.gl/1hs4bn - My question on SO: http://goo.gl/Sz2fV0 But picasso doesn't load image properly I sometimes get the image or placeholder image. What do you suggest? – TheDevMan Sep 28 '14 at 03:20
1
if(imageView.getDrawable() instanceof BitmapDrawable)
    bm = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
else { /***to check if it is ION drawable, use ION*/
    bm = Ion.with(imageView).getBitmap();
}
Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38