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 OnScroll
method 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();
}