Reputation: 841
I hate posting redundant questions, but I can't for the life of me figure out how to not have too much Bitmap-memory on the heap. It inflates perfectly on the first onCreate, but second time it fails. where/when do i recycle these things, @_o lol
I'm basically using an array list of Bitmaps, setting it to an adapter with a custom row layout, overriding getView.
**the bitmaps being used are jpeg's from the SD card // basically a vertically scrolling gallery.
mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
setListAdapter(getAdapter());
}//on create
private ArrayAdapter<Bitmap> getAdapter(){
ArrayList<Bitmap> picsII = new ArrayList<Bitmap>();
String DIRECTORY_PATH = "/sdcard/Mustaches/";
File file = new File(DIRECTORY_PATH);
final String[] s = file.list();
for(int i = 0; i<s.length; i++){
Bitmap b = BitmapFactory.decodeFile(DIRECTORY_PATH + s[i]);
picsII.add(b);
}
adapter = new ArrayAdapter<Bitmap>(this, R.layout.row, picsII){
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row;
if (null == convertView) {
row = mInflater.inflate(R.layout.row, null);
} else {
row = convertView;
}
ImageView iv = (ImageView) row.findViewById(R.id.imageView_row);
iv.setImageBitmap(getItem(position));
//some other widgets such as buttons and textviews
//from the custom row are inflated here
return row;
}
};
return adapter;
};
Upvotes: 1
Views: 1821
Reputation: 32748
Your problem is that you are eagerly loading up all the bitmaps when its likely you're only displaying a couple at a time.
That is you decode all the bitmaps into an array before you show any of them. This can easily exhaust all of your memory.
The approach you want to use is just load up the images for the ListView
rows that are being shown.
Look at this article for a better solution which involves loading the images in a background thread and maintaining a cache.
http://android-developers.blogspot.com/2010/07/multithreading-for-performance.html
Upvotes: 1