4

I have a scrollview which has small thumbnails of images loaded via AsyncTask and throws the image URL unto a imageView.

They are added dynamically and then on top, is a main imageView which holds the image of the thumbnail you clicked.

Everything runs great until you have about 10+ images in the thumbnails...

I am loading the mainImage url via the same way as the thumbnails, so when they click an image in the thumb, it loads it up top.

I am recycling the bitmap in the method itself, but it seems to be running out of memory and crashing when loading more than 10 images (thumbnails load ok, but crashes when i click to load the main image)

any help appreciated

this is the code i am using the load the images (thumbnails + main):

    private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
      ImageView bmImage;

      public DownloadImageTask(ImageView bmImage) {
          this.bmImage = bmImage;
      }

  protected Bitmap doInBackground(String... urls) {
      String urldisplay = urls[0];
      Bitmap mIcon11 = null;
      try {
        InputStream in = new java.net.URL(urldisplay).openStream();
        mIcon11 = BitmapFactory.decodeStream(in);
      } catch (Exception e) {
          Log.e("Error", e.getMessage());
          e.printStackTrace();
      }
      return mIcon11;
  }

  protected void onPostExecute(Bitmap result) {
      bmImage.setImageBitmap(result);
  }
    }
dmon
  • 29,684
  • 6
  • 84
  • 92
sykal
  • 61
  • 3
  • 9

8 Answers8

1

just implement this on ur image ... it will reduce ur image by 4 times

public static Bitmap getImage(byte[] image) {
        BitmapFactory.Options config = new BitmapFactory.Options();
        config.inPreferredConfig = Bitmap.Config.RGB_565;
        config.inSampleSize = 4;
        return BitmapFactory.decodeByteArray(image, 0, image.length,config);

    }
Nipun Gogia
  • 1,804
  • 1
  • 10
  • 16
1

You should read the two following Android official tutorials. They will teach you how to load large bitmap efficiently, and they provide working code samples that we use in our production apps

Displaying Bitmaps Efficiently

Loading Large Bitmaps Efficiently

znat
  • 12,164
  • 14
  • 60
  • 93
  • I should note, the images are very small (all within 50-60kb in size) i think i am going to check to make sure it's seeing the same size, and not using an absurd amount of memory instead- – sykal Nov 30 '12 at 17:57
1

you are not closing the input stream. So all the time when images urls executed input stream objects created which are expensive and causing you OutofMemoryError. add below code after catch block.

in.close();
in = null;
TNR
  • 6,014
  • 3
  • 30
  • 61
  • It's a good idea to close all the open things, but streams don't keep the data in memory. They pass data from input to output. They will also be garbage collected here since `in` goes out of scope after the `try` – zapl Nov 30 '12 at 14:44
0

You probably have to work with some sort of cache strategy for the bitmaps, try having a look on the library Universal Image Loader, it works very well for what you need

Recycling bitmaps by yourself should only be done when you know for sure that it won't be used anymore.

Alécio Carvalho
  • 12,697
  • 5
  • 63
  • 71
0

Try to resize the bitmap to a smaller size (with a factor like 1.5 example: width=widt*1.5) then strech(fit xy or whatever) in your view.it will lower the quality of the image due to the factor you decide. it is a quick dirty way. also making bit map rgb like Niun Goia mentions works well.

Ercan
  • 3,545
  • 1
  • 20
  • 36
0

You shouldn't be using a ScrollView for this. You should be using a ListView. When you use a ScrollView (with a LinearLayout or something like that inside it), all the elements in the list (no matter how big it is) will try to be loaded immediately. Try a ListView instead (which is made for that exact reason, it only loads in memory the ui elements of the rows that are visible).

Tas Morf
  • 2,965
  • 1
  • 10
  • 8
  • I really wanted to keep the images in a scrollview though because it would be nice to let the user scroll sideways through the thumbnails- – sykal Nov 30 '12 at 15:12
  • If you are set on doing this with a scrollview, you need to somehow implement yourself the caching of rows. For a good explanation of what I mean, have a look [here](http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/). A very good guide on building your own listview can be found [here](http://developer.sonymobile.com/2010/05/20/android-tutorial-making-your-own-3d-list-part-1/) and [here](http://developer.sonymobile.com/2010/05/31/android-tutorial-making-your-own-3d-list-part-2/). – Tas Morf Dec 05 '12 at 11:31
0

You can use android:largeHeap="true" to request a larger heap size

answer

Community
  • 1
  • 1
atasoyh
  • 2,655
  • 6
  • 26
  • 56
0

I use this..

Options opt = new Options();
opt.inPurgeable = true;
opt.inInputShareable = true;
opt.inPreferredConfig = Bitmap.Config.RGB_565;
atasoyh
  • 2,655
  • 6
  • 26
  • 56