【问题标题】:error in loading images to gridview android [duplicate]将图像加载到gridview android时出错[重复]
【发布时间】:2012-10-26 06:26:08
【问题描述】:

可能重复:
java.lang.OutOfMemoryError: bitmap size exceeds VM budget - Android

将图像加载到网格视图时出现以下错误。

java.lang.OutOfMemoryError: 位图大小超出 VM 预算

请帮助我。有没有解决这个错误的方法。

【问题讨论】:

  • 你的位图尺寸是多少。和你的SD卡尺寸>

标签: android gridview


【解决方案1】:

试试这个

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

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

【讨论】:

    猜你喜欢
    • 2015-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-04
    • 1970-01-01
    相关资源
    最近更新 更多