首页
学习
活动
专区
圈层
工具
发布

位图大
EN

Stack Overflow用户
提问于 2012-11-05 06:17:20
回答 2查看 5.2K关注 0票数 2

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

我用相机拍了一张照片,然后把照片保存在sdcard上,然后用

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英寸,宏碁iconia,a500,运行ics。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-11-05 06:20:57

试试这个

代码语言:javascript
复制
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;
    }

这将按您所传递的宽度和高度缩放位图。

票数 6
EN

Stack Overflow用户

发布于 2012-11-05 06:25:09

在将位图应用于图像视图之前,请使用此类将位图缩小到更小的所需大小。

代码语言:javascript
复制
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);
}
}

然后从你的活动中打电话

代码语言:javascript
复制
Bitmap reqBitmap = loadBitmap(String filePath,int requiredWidth,int requiredHeight) 

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13226907

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档