I am developing an android app which contains lot of bitmaps and their processing. I was getting frequent out of memory crashes. I searched a lot for correct and standard way to load bitmaps and the best one I got here Loading Large Bitmaps Efficiently
Now with this has reduced the frequency of crash but still there are some crashes to some specific area like, while displaying images stored in SDCARD to gridview, I am getting out of memory crash everytime even though I am using async task to load images.
I am adding code along with this questions, please have a look and suggest me what is wrong with the code :
PostExecute Method of LoadImage AsyncTask
protected void onPostExecute(final Bundle result) {
super.onPostExecute(result);
ImageView view = views.get(result.getInt("pos"));
view.setImageBitmap(BitmapFactory.decodeFile(result.getString("filePath")));
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(context, Monsterize.class);
i.putExtra("flagtwo", 3);
i.putExtra("backbgpath", filepath);
i.putExtra("backbgname", fileName);
i.putExtra("backbgpos", result.getInt("pos"));
i.putExtra("calledFrom", context.getClass().getSimpleName());
context.startActivity(i);
}
});
}
And Async taks is getting called from getview method of grid view adapter
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.gridview_item, null);
ImageView image = null;
if (vi != null) {
image = (ImageView) vi.findViewById(R.id.image);
}
Bundle b = new Bundle ();
b.putString("filePath", filepath[position]);
b.putInt("pos", position);
new LoadImage().execute(b);
views.put(position, image);
return vi;
}
views is a hashmap containing postion as key and Imageview as value. In async task images get set in imageview based on position.