0

I am trying to load images in res->drawable folder of my app into 12 GridViews(for every GridView almost 15 images).

I can load images into 7 of GridViews but after that outofmemory error occurs. I resized images and with this option I can load images in 7 GridView.

As I do not need to load all images, I tried to load some of images with handler class with 15 second delay but still outofmemory error occurs.

I used System.gc() and after use of image arrays I maked all of them null.

But still outofmemory error occurs. One thing that I think help me is that load images into GridViews when user Scroll Down for the certain position. B

But ScrollView class has no method for this and a protected OnScrollmethod is there and I do not think I can use that.

This is a code for loading images in GridView1. all of codes are like this.

 try {
    final GridviewAdapter mAdapter1;    
    final GridView  gridView1 = (GridView)findViewById(R.id.gridView1);


            ArrayList<String> listCountry1;
            ArrayList<Bitmap> listFlag1;

              listCountry1 = new ArrayList<String>();
              listFlag1 = new ArrayList<Bitmap>();



              Bitmap unscaledBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.w1);

              Bitmap scaledBitmap = Bitmap.createScaledBitmap(unscaledBitmap, dstwidth, dstheight, true);
              Bitmap unscaledBitmap2 = BitmapFactory.decodeResource(getResources(), R.drawable.w12);

              Bitmap scaledBitmap2 = Bitmap.createScaledBitmap(unscaledBitmap2, dstwidth, dstheight, true);
              Bitmap unscaledBitmap3 = BitmapFactory.decodeResource(getResources(), R.drawable.w15);

              Bitmap scaledBitmap3 = Bitmap.createScaledBitmap(unscaledBitmap3, dstwidth, dstheight, true);
              Bitmap unscaledBitmap4 = BitmapFactory.decodeResource(getResources(), R.drawable.w26);

              Bitmap scaledBitmap4 = Bitmap.createScaledBitmap(unscaledBitmap4, dstwidth, dstheight, true);
              Bitmap unscaledBitmap5 = BitmapFactory.decodeResource(getResources(), R.drawable.w28);

              Bitmap scaledBitmap5 = Bitmap.createScaledBitmap(unscaledBitmap5, dstwidth, dstheight, true);
              Bitmap unscaledBitmap6 = BitmapFactory.decodeResource(getResources(), R.drawable.w33);

              Bitmap scaledBitmap6 = Bitmap.createScaledBitmap(unscaledBitmap6, dstwidth, dstheight, true);
              Bitmap unscaledBitmap7 = BitmapFactory.decodeResource(getResources(), R.drawable.w44);

              Bitmap scaledBitmap7 = Bitmap.createScaledBitmap(unscaledBitmap7, dstwidth, dstheight, true);
              Bitmap unscaledBitmap8 = BitmapFactory.decodeResource(getResources(), R.drawable.w46);

              Bitmap scaledBitmap8 = Bitmap.createScaledBitmap(unscaledBitmap8, dstwidth, dstheight, true);
              Bitmap unscaledBitmap9 = BitmapFactory.decodeResource(getResources(), R.drawable.w50);

              Bitmap scaledBitmap9 = Bitmap.createScaledBitmap(unscaledBitmap9, dstwidth, dstheight, true);




                                           listCountry1.add("book0");
                                           listCountry1.add("book1");
                                           listCountry1.add("book2");
                                           listCountry1.add("book3");
                                           listCountry1.add("book4");
                                           listCountry1.add("book5");
                                           listCountry1.add("book6");
                                           listCountry1.add("book7");
                                           listCountry1.add("book8");
                                           //
                                           listFlag1.add(scaledBitmap);
                                           listFlag1.add(scaledBitmap2);
                                           listFlag1.add(scaledBitmap3);
                                           listFlag1.add(scaledBitmap4);
                                           listFlag1.add(scaledBitmap5);
                                           listFlag1.add(scaledBitmap6);
                                           listFlag1.add(scaledBitmap7);
                                           listFlag1.add(scaledBitmap8);
                                           listFlag1.add(scaledBitmap9);

                                               mAdapter1 = new GridviewAdapter(FirstPageActivity.this,listCountry1, listFlag1);


                                                gridView1.setAdapter(mAdapter1);







    // Implement On Item click listener
                               gridView1.setOnItemClickListener(new OnItemClickListener() 
    {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                long arg3) {
            Toast.makeText(FirstPageActivity.this, mAdapter1.getItem(position), Toast.LENGTH_SHORT).show();
        }
    });






                               gridView1.getViewTreeObserver().addOnGlobalLayoutListener( new OnGlobalLayoutListener() 
                        {
                        @Override
                        public void onGlobalLayout() 
                            {
                            gridView1.getViewTreeObserver().removeGlobalOnLayoutListener( this );
                            View lastChild = gridView1.getChildAt( gridView1.getChildCount() - 1 );
                            gridView1.setLayoutParams( new LinearLayout.LayoutParams( LayoutParams.FILL_PARENT, lastChild.getBottom() ) );
                            }
                        });

                               imageLoader1.clearDiscCache();
                              imageLoader1.clearDiskCache();
                              imageLoader1.clearMemoryCache();


                               unscaledBitmap=null;scaledBitmap=null;
                               unscaledBitmap2=null;scaledBitmap2=null;
                               unscaledBitmap3=null;scaledBitmap3=null;
                               unscaledBitmap4=null;scaledBitmap4=null;
                               unscaledBitmap5=null;scaledBitmap5=null;
                               unscaledBitmap6=null;scaledBitmap6=null;
                               unscaledBitmap7=null;scaledBitmap7=null;
                               unscaledBitmap8=null;scaledBitmap8=null;
                               unscaledBitmap9=null;scaledBitmap9=null;


                                  listFlag1=null;
                                  listCountry1=null;
                                  java.lang.System.gc();


    }
    catch (Exception e) {
        // TODO Auto-generated catch block
        Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
    }
androidboy
  • 15
  • 1
  • 7

2 Answers2

0

-First: you need setting heapsize of project to more (10 - 50 MB) this help your app -avoid outofmemory. - Second: you should resize original image to thumb image size (eg. 70x70 pixel) this help Bitmap decoder load smallest image to show on grid

So, you cand show your code here i could support more...

GiapLee
  • 436
  • 2
  • 8
  • i used large_heap="true" and hardware_acceleration="true" in manifest and i resized them to 120*120 and still outofmemory error occur when i want to load images in gridview8.this error occur when i want to load images in more than 7 gridviews and is not from gridview8. about my code : it is very large and very confusing but i add one of loading section codes in one gridview. – androidboy May 07 '15 at 09:21
0

Use some image loading library such as Picasso or Glide. You can use resize method to improve performance and to avoid out of memory errors. This tools will take care of reusing bitmaps and their caching.

https://github.com/square/picasso

https://github.com/bumptech/glide

With Picasso it's as easy as:

Picasso.with(context).load(R.drawable.drawableName).resize(50, 50).centerCrop().into(imageView);
Vit Veres
  • 774
  • 8
  • 11
  • Thanks you. but have can i use picasso to load image in customized gridview? – androidboy May 07 '15 at 09:13
  • Well I suppose that the grid item has some image view. And at the moment when you would assign the image to the imageview, just use Picasso. – Vit Veres May 07 '15 at 09:21
  • I used listFlag1.add(Picasso.with(getApplicationContext()).load(R.drawable.w1).get()); when listflag is array of bitmaps and passed as image source for gridviewadapter class. but when i use this, nothing show in gridview1. why it is happen? what i do wrong? – androidboy May 07 '15 at 09:37
  • This way it won't work, because picasso won't return bitmap immediately - it's an async tool. You should use it in the way i posted above. For this you will need to implement custom adapter and custom view, to have more controll over the drawing.. Check: http://stackoverflow.com/a/19699602/2951744 – Vit Veres May 07 '15 at 10:51
  • Can you please say me about async tool? – androidboy May 07 '15 at 12:59
  • With that i just meant that the Picasso downloads and puts image to the target image view asynchronously. Try to follow the guide i sent above. – Vit Veres May 07 '15 at 13:04