ViewPager Fragment displaying Memory Error for loading ImageViews

60 views Asked by At

I have a ViewPager with a Fragment coming from a PagerAdapter. The Fragment has 4 ImageViews which I want to set their backgrounds with images located in my Drawable folder. The drawables are about 80dp with size about 30kb each. I used the following to populate my ImageViews, where MyIMGS is an array of ImageViews.

switch (Mynum)
{
case 0: 

    int Array1[] = args.getIntArray("Array_A");
    for(int i=0;i<Array1.length;i++)
    {
        MyIMGS[i].setBackgroundResource(Array1[i]);

    }
    break;
case 1:
    int Array2[] = args.getIntArray("Array_A");
    for(int i=0;i<Array2.length;i++)
    {
        MyIMGS[i].setBackgroundResource(Array2[i]);

    }
    break;
    }

The First Problem is that after an intensive back and forth with back pressed a nice message outofMemory is displayed after a while. I tried the following solution

private static Bitmap decodeResource(Resources res, int resId ,int reqWidth,int reqHeight)
{
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);

// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);

}

And

IMGS[i].setImageBitmap(decodeResource(getResources(), Array1[i], 100, 100));

By using the above the NEW problem is that my 20 images from Drawable folder are displayed one on top of another on the 4 imageviews. Its like the For Loop is executed not for the MyIMGS array but and for each element separately!?. My Questions are : For such small files do I need the decode Resource, and If yes why Images are displayed in such a way.

1

There are 1 answers

0
The Original Android On

I suspect the array elements of MyIMGS is an ImageView defined in the same location. Perhaps you should post the code for MyIMGS and its layout xml file.