Sujith
Sujith

Reputation: 7753

Custom GridView causes out of memory error while loading lots of images

I have a custom layout for gridview. Each raw of gridview contains a progressbar, two imageviews and two text views. The image size are thumbnail sizes. While i have to load lots of bitmaps eg:- 500 images , which causes out of memory error. the images are first time loading from Internet and then which is stored in SD card and the next time when you loading the gridview which is loading from SD card. How to overcome this issue. I have found lots of answers to overcome out of memory if the gridview is inflated with single imageview. Please suggest me how to overcome this issue while using custom layout.Let me know if you know any example projects that handle this out of memory error in gridview while inflating custom layout in gridview.

Upvotes: 0

Views: 2117

Answers (4)

sandeep kumar
sandeep kumar

Reputation: 11

Solve the out of memory exception issue with this code

Bitmap bitmap = BitmapFactory.decodeStream(is);
if (bitmap != null) {
    Bitmap resizedBitmap = Bitmap.createScaledBitmap(bitmap, 100, 100, true);
    imageView = new ImageView(mContext);
    imageView.setImageBitmap(resizedBitmap);
}

Upvotes: 0

AnilPatel
AnilPatel

Reputation: 2366

try this code set in image for gridview out of memory

BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize = 8;//you set size qulity for image(2,3,,4,5,6,7,8 etc..) 
Bitmap preview_bitmap=BitmapFactory.decodeStream(is,null,options);

set this bitmap in your imageview

Upvotes: 0

Peter Lo
Peter Lo

Reputation: 110

I am facing the same issue. The OutOfMemoryError is usually caused by The first approach is to

BitmapFactory.decodeFile(srcImg);

Since images are transformed into bitmap before displaying, lots of big bitmap usually cause the error.

To overcome this, I added the following function

public static Bitmap decodeWithBounds(String srcImg, int bounds) {
    if (bounds > 0){
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(srcImg, options);

        if (options.outHeight > bounds || options.outWidth > bounds){
            options.inSampleSize = Math.max(options.outHeight/bounds, options.outWidth/bounds);
        }
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(srcImg, options);
    } else {
        return BitmapFactory.decodeFile(srcImg);
    }
}

I use this function to decode bitmap, with bounds = grid size.

This solved most problem.

For very low end device, add a try{} catch (OutOfMemoryError e){} ...

Upvotes: 0

KOTIOS
KOTIOS

Reputation: 11194

It would be best to create a class that extends Application . This application class will give you onlowmemory() callback whenever application goes low memory. on there you can write

public void onLowmemory() {

Runtime.getRuntime().gc(); }

which will invoke system GC method. Upon executing garbage collector android will garbage all unused objects.

There is another way to solve this problem. In animation you can call Runtime.getRuntime().gc(); to invoke garbage collector. also in activity onDestroy() method u can call Runtime.getRuntime().gc();

so your problem will be solved

Upvotes: 2

Related Questions