0

So I'm loading images into Android Gallery from SD card. I hit menu and then select and it's inserted into Gallery via code below. Problem is after about 5 or 6 images I get a out of memory error 01-04 18:10:35.246: ERROR/AndroidRuntime(10220): java.lang.OutOfMemoryError: bitmap size exceeds VM budget Any ideas on what I can do to resolve this?

public class ImageAdapter extends BaseAdapter{  

    int mGalleryItemBackground;  
    public ImageAdapter(Context c)  {     
        mContext = c;     
        TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme);
        mGalleryItemBackground = typArray.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground, 0);
        typArray.recycle();
    }  
    public int getCount(){  
        return mUrls.length;  
    }  
    public Object getItem(int position){  
        return position;  
    }  
    public long getItemId(int position) {  
        return position;  
    }  
    public View getView(int position, View convertView, ViewGroup parent){  
        ImageView i = new ImageView(mContext);  

        i.setImageURI(mUrls[position]); 
        i.setScaleType(ImageView.ScaleType.FIT_XY);  
        i.setLayoutParams(new Gallery.LayoutParams(120, 120));  
        return i;  
    }     
    private Context mContext;  
    }     
satur9nine
  • 13,927
  • 5
  • 80
  • 123
Paul
  • 1,714
  • 7
  • 22
  • 45
  • Try removing recycle method. It solved the issue for me. Not sure it is the best practice though. – GSree Jan 05 '11 at 00:19

2 Answers2

0

What is the width and height in pixels of the images you are inserting into the Gallery? If they are very large, perhaps 500x500 or larger then I'd suggest resizing them to something smaller and closer to your intended size of 120x120 before adding them to the Gallery.

You can scale them down using the method Bitmap.createScaledBitmap.

EDIT: Note that this is different from having the ImageView do the scaling. The ImageView will scale the bitmap as it is displayed but keeps the original bitmap in heap memory. If you use createScaledBitmap and throw the original bitmap away then you'll be saving a lot of heap.

satur9nine
  • 13,927
  • 5
  • 80
  • 123
  • For some reason everytime I try to convert the uri i.setImageURI(mUrls[position]) into a bitmap I get a nullpointer excception.. – Paul Jan 05 '11 at 03:22
  • To convert the URL into a bitmap take a look at either or both of these two posts: http://stackoverflow.com/questions/3118691/android-make-an-image-at-a-url-equal-to-imageviews-image http://stackoverflow.com/questions/2471935/how-to-load-an-imageview-by-url-in-android – satur9nine Jan 05 '11 at 06:17
  • I know how to do this as I've done it elsewhere in my app but for some reason I get that error when I try to call via position if I use the actual integer say 0 it works albeit strangely but I'll give you the answer as scaling seems to be the answer – Paul Jan 05 '11 at 15:18
-1

Try to avoid usage of

private Context mContext;

It's very bad practice to store Context/Activity as private members of classes. Context/Activity recommended to use only through parameters. Typical result of storing Context is a memory leaks, which I believe demonstrates your code. More you can read here.

Barmaley
  • 16,638
  • 18
  • 73
  • 146
  • Actually if you look at this article closely you'll see that "private Context mContext" is perfectly safe. The article indicates that "static" would cause a problem, because static is going to live beyond the lifecycle of the Activity object and result in a leak. This whole article actually seems to indicate a little bit of an architecture mishap, why does Google recreate the Activity when the screen rotates, not the best design IMHO. – satur9nine Jan 05 '11 at 18:17
  • Nope, I'd disagree. Subject not in static or not static ones, but for preserving in living object instances old/died/destroyed activities/contexts. So if "private Context mContext" lives/dies in the same way as actual context - it's safe, otherwise it's unsafe. – Barmaley Jan 05 '11 at 20:16