1

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

Achayan
  • 5,720
  • 2
  • 39
  • 61
  • Did you go through the Google-recommended approach? - https://developer.android.com/training/displaying-bitmaps/manage-memory.html – Srikanth Sep 12 '14 at 05:12
  • check out my answer to resolve out of memory error on followed link http://stackoverflow.com/questions/7737415/out-of-memory-error-with-images/21669709#21669709 – Jigar Sep 12 '14 at 05:12
  • use Picasso library see the question here... ...http://stackoverflow.com/a/23865531/3535286 – chiragkyada Sep 12 '14 at 05:14
  • also try `options.inSampleSize = 2; bitmap= BitmapFactory.decodeResource(in, null, options);`. `inSampleSize` could be in power of 2s(2, 4, 8, etc) – Andro Sep 12 '14 at 05:15
  • you can use image loader library or Google Aquery library for your image loading. – Divyang Metaliya Sep 12 '14 at 05:20
  • thank you all ... I got my prob solved – Achayan Sep 12 '14 at 05:24

3 Answers3

1

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);
Jigar
  • 791
  • 11
  • 21
1
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:-
Dakshesh Khatri
  • 639
  • 7
  • 12
0

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"
Jitty Aandyan
  • 1,994
  • 1
  • 13
  • 12