2

Possible Duplicate:
“Bitmap too large to be uploaded into a texture”

I am taking a picture using the camera, then I am saving the picture on the sdcard, then using

Bitmap bm = BitmapFactory.decodeFile(path);

To get the bitmap, then

imageView.setImageBitmap(bm); to set it.

But when I put it into my view using

mFrameLayout.addView(imageView); no image is displayed and I get back Bitmap to large to be uploaded into texture

The bitmap is 1944 high, 2592 wide

Any thoughts?

I am using a tablet, 10.1 inches, acer iconia, a500, running ics.

Community
  • 1
  • 1
FabianCook
  • 20,269
  • 16
  • 67
  • 115
  • have you checked this post: http://stackoverflow.com/questions/10271020/bitmap-too-large-to-be-uploaded-into-a-texture ? – Bruno Bieri Nov 05 '12 at 06:19
  • this is a common error, currently android supports only upto 2000px , u are lucky not to get a crash on this because of out of error... – Amit Hooda Nov 05 '12 at 06:19
  • http://stackoverflow.com/questions/12108841/how-to-catch-error-bitmap-too-large-to-be-uploaded-into-a-texture – Krishnabhadra Nov 05 '12 at 06:20
  • you can re-size image then use it see [this](http://stackoverflow.com/a/11689101/1289716) – MAC Nov 05 '12 at 06:20
  • http://stackoverflow.com/a/13226838/1487822 – Parag Ghetiya Nov 05 '12 at 06:25
  • it not only depend on Bitmap height and width.. some times bitmap size also .. a small image contain more number of pix contain more mb then large image.. so you can resize the image.. – Sandeep P Nov 05 '12 at 06:31

2 Answers2

6

try with this

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;
    }

this will scale bitmap as per width and height you pass

Sanket Kachhela
  • 10,861
  • 8
  • 50
  • 75
2

Use this class to scale down your bitmap to a smaller required size before applying it in the image view.

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;


class BitmapLoader
{
    public static int getScale(int originalWidth,int originalHeight,
    final int requiredWidth,final int requiredHeight)
    {
       //a scale of 1 means the original dimensions 
       //of the image are maintained
      int scale=1;

//calculate scale only if the height or width of 
//the image exceeds the required value. 
if((originalWidth>requiredWidth) || (originalHeight>requiredHeight)) 
{
    //calculate scale with respect to
    //the smaller dimension
    if(originalWidth<originalHeight)
        scale=Math.round((float)originalWidth/requiredWidth);
    else
      scale=Math.round((float)originalHeight/requiredHeight);

}

return scale;
}

public static BitmapFactory.Options getOptions(String filePath,
    int requiredWidth,int requiredHeight)
{

BitmapFactory.Options options=new BitmapFactory.Options();
//setting inJustDecodeBounds to true
//ensures that we are able to measure
//the dimensions of the image,without
//actually allocating it memory
options.inJustDecodeBounds=true;

//decode the file for measurement
BitmapFactory.decodeFile(filePath,options);

//obtain the inSampleSize for loading a 
//scaled down version of the image.
//options.outWidth and options.outHeight 
//are the measured dimensions of the 
//original image
options.inSampleSize=getScale(options.outWidth,
        options.outHeight, requiredWidth, requiredHeight);

//set inJustDecodeBounds to false again
//so that we can now actually allocate the
//bitmap some memory
options.inJustDecodeBounds=false;

return options;

}



public static Bitmap loadBitmap(String filePath,
    int requiredWidth,int requiredHeight){


BitmapFactory.Options options= getOptions(filePath,
        requiredWidth, requiredHeight);

return BitmapFactory.decodeFile(filePath,options);
}
}

Then from your activity, call

Bitmap reqBitmap = loadBitmap(String filePath,int requiredWidth,int requiredHeight) 

method of this class providing the filepath of the bitmap obtained from the sd card, and setting the requiredWidth and requiredHeight to the dimensions you wish to scale the bitmap to. Now use the reqBitmap.

Vikram Gupta
  • 6,496
  • 5
  • 34
  • 47