0

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.

Brijesh
  • 59
  • 1
  • 2
  • 5

3 Answers3

0

You can use universal image loader for this work it will handle memory related issues. go through below link.

Android Universal Image loader

Faraz Ahmed
  • 1,245
  • 1
  • 14
  • 24
0
views.put(position, image);

views is a hashmap containing postion as key and Imageview as value

And here is the problem. While the grid view is managing its views, reusing them, so that only the visible ones are occupying memory, you store all your image views in the memory. It's not clear why you did this - views is never used in your source - but it references all of the images all the time.

stan0
  • 11,549
  • 6
  • 42
  • 59
-1

Firstly, you decode the Bitmap in onPostExecute. It makes your app slower as this method is executed on the UI thread.

Bitmaps should be decoded in the doInBackground method and set to the ImageView in onPostExecute.

Secondly, in an adapter, views are created and destroyed as the user scrolls in the GridView.

You might want to use an HashMap<String, Bitmap> to store the bitmaps and avoid decoding the same bitmap twice. Use the file path as key and the bitmap as value.