Achayan
Achayan

Reputation: 5885

Loading multiple images to gridview android

I have around 300 jpg images and I want to load this images to a grid view. I am following a tutorial

But when I try to load images ( if its less than 25 its fine ) then I am getting outofmemory error.

private ArrayList getData() {
        final ArrayList imageItems = new ArrayList();
        // retrieve String drawable array
        TypedArray imgs = getResources().obtainTypedArray(R.array.image_ids);
        for (int i = 0; i < imgs.length(); i++) {
            Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(),
                    imgs.getResourceId(i, -1));
            imageItems.add(new ImageItem(bitmap, "Image#" + i));
        }

        return imageItems;

    }

This is the code to load the images and

customGridAdapter = new GridViewAdapter(this, R.layout.row_grid, getData());
        gridView.setAdapter(customGridAdapter);

This is the code to set the images. Do you guys have any idea how I can load all my ( 200+ images ) to my grid view ?

Thanks

Upvotes: 1

Views: 1609

Answers (3)

Dakshesh Khatri
Dakshesh Khatri

Reputation: 629

For solving OutOfMemory use below link library :-

Download jar and put in libs folder.

http://square.github.io/picasso/


then you should use below code to load image into imageview either from url or drawable folder.

Picasso.with(context).load("your url or drawable image path").into(imageView);


This is best library for solving outofmemory issues:-

Upvotes: 1

Jitty Aandyan
Jitty Aandyan

Reputation: 2004

View Holder save your layout in memory to save time and processing for inflating view again,so if you have lots of images to load then it will fill your memory and gives you out of memory. best way to fix this is try to rescale image to small resolution image and set it to imageview or remove view holder. One more suggestion add largeHeap=true and hardwareAccelerated=true in application tag inside AndoirdManifest.xml

    android:hardwareAccelerated="true"
    android:largeHeap="true"

Upvotes: 0

Jigar
Jigar

Reputation: 791

For Solving java.lang.OutOfMemoryError Exception at android.graphics.BitmapFactory.nativeDecodeByteArray, you should use Following Code:

BitmapFactory.Options options=new BitmapFactory.Options();// Create object of bitmapfactory's option method for further option use
                options.inPurgeable = true; // inPurgeable is used to free up memory while required
                Bitmap songImage1 = BitmapFactory.decodeByteArray(thumbnail,0, thumbnail.length,options);//Decode image, "thumbnail" is the object of image file
                Bitmap songImage = Bitmap.createScaledBitmap(songImage1, 50 , 50 , true);// convert decoded bitmap into well scalled Bitmap format.

imageview.SetImageDrawable(songImage);

Upvotes: 1

Related Questions