Robi Kumar Tomar
Robi Kumar Tomar

Reputation: 3404

OutOfMemoryError on GridView scrolling in android

Hitting OutOfMemoryError while scrolling the gridview.

Adapter code:

    package com.example.gridviewexample;

import android.content.*;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter {
    private Context mContext;


    private  Integer arrayElement[] = {
             R.drawable.rkt, R.drawable.images,R.drawable.sir,R.drawable.sir2,
             R.drawable.sir3,R.drawable.sir4, R.drawable.images1,
            R.drawable.images3, R.drawable.indeerx,
            R.drawable.index,R.drawable.rkt2,R.drawable.rkt3,R.drawable.rkt4,R.drawable.rkt5,R.drawable.sir2,
            R.drawable.sir3, 
    };


    public ImageAdapter(Context c){
        mContext = c;
    }

    @Override
    public int getCount() {
        return arrayElement.length;
    }

    @Override
    public Integer getItem(int position) {
        return arrayElement[position];
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView = new ImageView(mContext);
        imageView.setImageResource(arrayElement[position]); 
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setLayoutParams(new GridView.LayoutParams(200,200));
        return imageView;
    }

}

Any Suggestion would be appreciated!

Upvotes: 0

Views: 1156

Answers (3)

Bilal Jarral
Bilal Jarral

Reputation: 9

this is a android bug out of memory ...b/c android heap memory cannot support large siz ...so first step u creat drawable folder put all images in drawable folder then all images extention .jpg b/c .png have alrg size and try to post this code in manifest file in application tag

android:largeHeap="true"

Upvotes: 0

Quamber Ali
Quamber Ali

Reputation: 2171

Its a known bug, its not because of large files. Since Android Caches the Drawables, its going out of memory after using few images. But i found alternate way for it, by skipping the android default cache system.

Soultion: Create a drawable folder in Assets and move the images to "drawable" folder in assets and use the following function to get BitmapDrawable

public static Drawable getAssetImage(Context context, String filename) throws IOException {
    AssetManager assets = context.getResources().getAssets();
    InputStream buffer = new BufferedInputStream((assets.open("drawable/" + filename + ".png")));
    Bitmap bitmap = BitmapFactory.decodeStream(buffer);
    return new BitmapDrawable(context.getResources(), bitmap);
}

Refrence : https://stackoverflow.com/posts/6116316/revisions

Upvotes: 0

Lia Pronina
Lia Pronina

Reputation: 756

Try to do this:

public View getView(int position, View convertView, ViewGroup parent) {
    View grid = convertView;

    if (convertView == null) {
        grid = inflater.inflate(R.layout.grid1, parent, false);
    }

    TextView item = (TextView) grid.findViewById(R.id.item_title);
    item.setBackgroundResource(mThumbIds[position]);

    return grid;
}

I use TextView to display the images, you can try to change TextView to ImageView for yourself.

Upvotes: 2

Related Questions