位图到大

时间:2012-11-05 06:17:20

标签: android bitmap

  

可能重复:
  “Bitmap too large to be uploaded into a texture”

我正在使用相机拍照,然后我将图片保存在SD卡上,然后使用

Bitmap bm = BitmapFactory.decodeFile(path);

要获取位图,那么

imageView.setImageBitmap(bm);设置它。

但是当我使用

将它放入我的视图时

mFrameLayout.addView(imageView);没有显示图片,我回来了Bitmap to large to be uploaded into texture

位图为1944 high, 2592 wide

有什么想法吗?

我正在使用平板电脑,10.1英寸,acer iconia,a500,运行ics。

2 个答案:

答案 0 :(得分:6)

试试这个

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

这将根据您传递的宽度和高度缩放位图

答案 1 :(得分:2)

在将图像应用到图像视图中之前,请使用此类将位图缩小到所需的较小尺寸。

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

然后从您的活动中致电

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

此类的方法,提供从SD卡获取的位图的文件路径,并将requiredWidth和requiredHeight设置为您希望将位图缩放到的维。现在使用reqBitmap。