java - Android out of memory error when using ListView and Bitmap -


i have used viewholder + bitmap decodes/compress , imagecache in listview can no longer have out of memory error unfortunately i'm still getting it, happens when i'm listview activity activity, or when go current listview activity listview activity. can please tell me if i'm missing something? here's code:

private class myadapter extends arrayadapter<string> {       lrucache<integer, bitmap> imagecache;          public myadapter(context context, int resource, int textviewresourceid, string[] strings) {             super(context, resource, textviewresourceid, strings);              final int maxmemory = (int) (runtime.getruntime().maxmemory() / 1024);             final int cashesize = maxmemory / 8;             imagecache = new lrucache<>(cashesize);          }          @override         public view getview(int position, view convertview, viewgroup parent) {              viewholderitem viewholder;             string[] names = getresources().getstringarray(r.array.grilled_fresh_beef_names);             string[] prices = getresources().getstringarray(r.array.grilled_fresh_beef_prices);             string[] ingredients = getresources().getstringarray(r.array.grilled_fresh_beef_ingredients);              if(convertview==null) {                 layoutinflater inflater = (layoutinflater) getsystemservice(context.layout_inflater_service);                 convertview = inflater.inflate(r.layout.categories_item, parent, false);                   viewholder = new viewholderitem();                  viewholder.item_name = (textview) convertview.findviewbyid(r.id.items_name);                 viewholder.item_price = (textview) convertview.findviewbyid(r.id.item_price);                 viewholder.item_ingredient = (textview) convertview.findviewbyid(r.id.ingredients);                 viewholder.imageview = (imageview) convertview.findviewbyid(r.id.item_image);                 viewholder.imageview.setscaletype(imageview.scaletype.center_crop);                  convertview.settag(viewholder);              } else {                 viewholder = (viewholderitem) convertview.gettag();             }            viewholder.item_name.settext(names[position]);             viewholder.item_price.settext(prices[position]);             viewholder.item_ingredient.settext(ingredients[position]);                bitmap bitmap = imagecache.get(position);             if (bitmap == null) {                 bitmap = bitmapfactory.decoderesource(getresources(), beef_burger[position]);                 bytearrayoutputstream bos = new bytearrayoutputstream();                 bitmap.compress(bitmap.compressformat.jpeg, 100, bos);                   imagecache.put(position, bitmap);             }              viewholder.imageview.setimagebitmap(bitmap);              return convertview;         }        } 

logcat

the problem using lru cache way keep reference bitmaps create 1 ones not on screen. android not able garbage collect these , resuse them because of reference lru cache.

as other answer stated better use library take care of caching you. recommend picasso or glide.

i use glide. seems load images bit faster , perfect loading images in listview.

its simple

glide.with(context)     .load(***image file/path/bitmap***)     .into(viewholder.imageview); 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -