0

I want variable background to GridView and I run my app on a 800*480 resolution device. I am able to add variable background, but after some time I am getting OutOfMemoryError. I need to add that bitmap for each shelf and shelf has 800*200 dimensions.

I have tried bitmap.recycle()
and also

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), R.id.myimage, options);
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
String imageType = options.outMimeType;

and also

    public static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {
        if (width > height) {
            inSampleSize = Math.round((float)height / (float)reqHeight);
        } else {
            inSampleSize = Math.round((float)width / (float)reqWidth);
        }
    }
    return inSampleSize;
   }
   public static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {
        if (width > height) {
            inSampleSize = Math.round((float)height / (float)reqHeight);
        } else {
            inSampleSize = Math.round((float)width / (float)reqWidth);
        }
    }
    return inSampleSize;
   }

But still I am unable to get rid of that OutOfMemory error. Please help me.

Adinia
  • 3,722
  • 5
  • 40
  • 58
indraja machani
  • 679
  • 1
  • 9
  • 25
  • start [debugging](http://android-developers.blogspot.de/2011/03/memory-analysis-for-android.html) with [MAT](http://stackoverflow.com/a/6080980/995891) for example. There is no simple solution here. – zapl Sep 26 '12 at 12:24
  • Please see my generic [answer](http://stackoverflow.com/questions/10737582/bitmap-size-exceeds-vm-budget-error-android/10738115#10738115) on same issue. And don't forget to upvote my answer if it solves your problem. :) – Shrikant Ballal Sep 26 '12 at 12:25
  • @Shrikant:I have seen your answer & installed MAT,checked where the memory leak is happening.the thing is I have an app with 7 activities & in these 7 has grid of images,books etc.and Here I am adding variable background to grid view.the thing is transition among the activities is done by left and right swiping..when i swipe continuously among the activities,and at 50th time swiping I am getting this error..If u understand my bug,please debug it.And the thing is I have tried using all kinds of methods to get rid of this like by using options,weak reference, bitmap.recycle(),system.gc().etc.. – indraja machani Sep 27 '12 at 07:52

1 Answers1

0

Its Working Fine For Me.

    public static Bitmap decodeSampledBitmapFromResource(Resources res,
                int resId, int reqWidth, int reqHeight) {

            // First decode with inJustDecodeBounds=true to check dimensions
            final BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            options.inDither=false;                     //Disable Dithering mode
            options.inPurgeable=true;                   //Tell to gc that whether it needs free memory, the Bitmap can be cleared
            options.inInputShareable=true;  
            options.inTempStorage=new byte[32 * 1024]; 
            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);
        }

        public static int calculateInSampleSize(BitmapFactory.Options options,
                int reqWidth, int reqHeight) {
            // Raw height and width of image
            final int height = options.outHeight;
            final int width = options.outWidth;
            int inSampleSize = 1;

            if (height > reqHeight || width > reqWidth) {
                if (width > height) {
                    inSampleSize = Math.round((float) height / (float) reqHeight);
                } else {
                    inSampleSize = Math.round((float) width / (float) reqWidth);
                }
            }
            return inSampleSize;
        }
Rishabh Agrawal
  • 861
  • 2
  • 15
  • 25