0

I'm trying to set an image as a background to a view (PiePlot) but I'm getting OutOfMemory exception.

Bg image size is 170kb.
I tried 5kb sample image for background and it works without exception.

I tried following :

@Override
protected void onDestroy() {
    super.onDestroy();

    unbindDrawables(mView);
    System.gc();
}

private void unbindDrawables(View view) {
    if (view.getBackground() != null) {
        view.getBackground().setCallback(null);
    }
    if (view instanceof ViewGroup) {
        for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
            unbindDrawables(((ViewGroup) view).getChildAt(i));
        }
        ((ViewGroup) view).removeAllViews();
    }
}

But this is useful if/when onDestroy() called. But while starting the app, this won't work and hence app crashes.

I tried this also:

BitmapDrawable bitmapDrawable = (BitmapDrawable) ctx.getResources().getDrawable(R.drawable.bg2);
BitmapFactory.Options bitopt = new BitmapFactory.Options();
bitopt.inSampleSize = 10;
plot.setBackgroundImage(bitmapDrawable); //plot is PiePlot object

But same result i.e. app crashes.

Any help appreciated.

GAMA
  • 5,958
  • 14
  • 79
  • 126

2 Answers2

1

try by putting this function...

public static Bitmap decodeFile(File f,int WIDTH,int HIGHT){
     try {
         //Decode image size
         BitmapFactory.Options o = new BitmapFactory.Options();
         o.inJustDecodeBounds = true;
         BitmapFactory.decodeStream(new FileInputStream(f),null,o);

         //The new size we want to scale to
         final int REQUIRED_WIDTH=WIDTH;
         final int REQUIRED_HIGHT=HIGHT;
         //Find the correct scale value. It should be the power of 2.
         int scale=1;
         while(o.outWidth/scale/2>=REQUIRED_WIDTH && o.outHeight/scale/2>=REQUIRED_HIGHT)
             scale*=2;

         //Decode with inSampleSize
         BitmapFactory.Options o2 = new BitmapFactory.Options();
         o2.inSampleSize=scale;
         return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
     } catch (FileNotFoundException e) {}
     return null;
 }
Mehul Ranpara
  • 4,245
  • 2
  • 26
  • 39
  • what to pass as 3 parameters? – GAMA Dec 07 '12 at 07:07
  • chk accepted answer of http://stackoverflow.com/questions/10314527/caused-by-java-lang-outofmemoryerror-bitmap-size-exceeds-vm-budget ... What should I pass as file object? – GAMA Dec 07 '12 at 07:08
1

just implement this on ur image ... it will reduce ur image by 4 times

public static Bitmap getImage(byte[] image) {
        BitmapFactory.Options config = new BitmapFactory.Options();
        config.inPreferredConfig = Bitmap.Config.RGB_565;
        config.inSampleSize = 4;
        return BitmapFactory.decodeByteArray(image, 0, image.length,config);

    }
Nipun Gogia
  • 1,846
  • 1
  • 11
  • 17
  • what parameter should be passed to this method? I have `BitmapDrawable` object.... And how to decide **inSampleSize Factor** , You have suggested `4` here... – GAMA Dec 07 '12 at 07:06
  • inSampleSize value means your image will reduce *** times .... here i give 4 which means my image will reduce by 4 times to actual image you have to pass your image as parameter – Nipun Gogia Dec 07 '12 at 08:08
  • I know the functionality of `inSampleSize` but how to calculate it's value for particular application. – GAMA Dec 07 '12 at 10:58
  • just check it out the accepted answer of this link....... http://stackoverflow.com/questions/10314527/caused-by-java-lang-outofmemoryerror-bitmap-size-exceeds-vm-budget – Nipun Gogia Dec 11 '12 at 06:12
  • first 3 pts suggests how system deals with the situation when available memory is getting low i.e. **after** the app is started. But I'm unable to initialize the app even after trying `inSampleSize`... – GAMA Dec 11 '12 at 06:18
  • just implement try/catch n see what it sends back ....dont forget to write message in catch block – Nipun Gogia Dec 11 '12 at 06:23