0

I have an adapter which extends from BaseAdapter and in this, I am trying to display a set of images. Before rendering the images, I have to dynamically change the width and height of each image and I am able to accomplish it by dynamically loading image from an external URL and then modifying the resulting bitmap's width and height.

In case if any of the image fetch fails, I have a backup image which needs to be rendered. This is stored in "res" folder as part of the resources. Currently, I am doing this in the adapter constructor:

Drawable drawable = context.getResources().getDrawable(R.drawable.backup);
Bitmap  backupBitmap = ((BitmapDrawable)drawable).getBitmap();

and in the getView method, I dynamically change the width and height of this backup bitmap as well since I want it to have the same width and height as others in the list.

Please let me know if this approach is correct or if I need to change something? Will the backupBitmap cause memory or performance issues? Please advise.

Raghav
  • 1,014
  • 2
  • 16
  • 34
  • Instead of drawable to bitmap conversion you can simply keep backup image as a by default image for image in list view. – VVB Jan 08 '16 at 17:51
  • @VVB: If I have to change the width and height of the image, then shouldn't it be converted to bitmap for the same? – Raghav Jan 08 '16 at 17:58
  • Yes If you keep desired width/height image in res folder then you don't need to convert it into bitmap. Simply, it will keep showing this as a default image if your WS fails to get image for specific URL – VVB Jan 08 '16 at 18:02
  • Thanks for your comment but in my case I don't know the width and height beforehand. I want the default width and height to be adjusted just like other images in the list. If I am loading n images and only one fails, the failed image size should be similar to other images – Raghav Jan 08 '16 at 18:38
  • But you can keep probable size of the default image. Because you can get to know the generally receiving sizes of images from your server – VVB Jan 08 '16 at 18:50
  • Yeah I am doing that. The default image is set by default in XML and is set to probable size of the images. Once I get other images, in getView, I want to change the default image's size. So, for that, instead of doing the drawable to bitmap conversion in getView() , I moved it to constructor. – Raghav Jan 08 '16 at 18:54
  • 1
    Yes that is the correct way – VVB Jan 08 '16 at 18:55
  • Thanks for the confirmation – Raghav Jan 08 '16 at 18:56

1 Answers1

1

Make sure to recycle the data when moving out of view, or you may run into issues when having lots of bitmaps in memory.

See documentation below:

http://developer.android.com/training/displaying-bitmaps/manage-memory.html

lawonga
  • 832
  • 10
  • 20
  • Though the link provides useful info, it does not specifically address the problem posed by the question above. Thanks for your reply anyway. – Raghav Jan 08 '16 at 18:56