Android:Bitmap_NJI - 无法创建SkBitmap

时间:2012-09-06 18:04:34

标签: java android

所以我有一个使用decodeFile加载图像的函数 - 然后我调整照片大小并将其绘制到画布中。这在我的Moto Droid 2.2.3&我的Nexus 4.1.1 - 但是在每次createScaledBitmap触发时我的HTC 4.0.3崩溃并抛出错误“无法创建SkBitmap”......

这是代码示例...

// create image bitmap
            BitmapFactory.Options bmOptions = new BitmapFactory.Options();
            bmOptions.inPurgeable = true;
            bmOptions.inSampleSize = 2;
            Bitmap rawImage = BitmapFactory.decodeFile(imageURL,bmOptions);
            Bitmap bmp = Bitmap.createBitmap(rawImage);

            float ratio = 0;
            int maxHeight = 900;
            int maxWidth = 900;
            float height = bmp.getHeight();
            float width = bmp.getWidth();

            Log.d("HEIGHT",""+height);
            Log.d("WIDTH",""+width);
            int nh = 0;
            int nw = 0;
            // check if current width is larger
            if(width > maxWidth){
                ratio = maxWidth / width;
                float newWidth = maxWidth;
                float newHeight = height*ratio;

                nw = Math.round(newWidth);
                nh = Math.round(newHeight);

                Log.d("NEW RATIO",""+ratio);
                Log.d("NEW HEIGHT",""+newHeight);

                // RESET HEIGHT AND WIDTH
                height = height * ratio;
                width = width * ratio;
            }
            if(height > maxHeight){
                ratio = maxHeight / height;

                float newWidth = width*ratio;
                float newHeight = maxHeight;

                nw = Math.round(newWidth);
                nh = Math.round(newHeight);


                Log.d("NEW RATIO",""+ratio);
                Log.d("NEW HEIGHT",""+newHeight);
                height = height * ratio;
                width = width * ratio;
            }
            bmp = Bitmap.createScaledBitmap(bmp,nw,nh,false);
            Bitmap img = Bitmap.createBitmap(bmp,0,0,655,655);

就像我说的,这在我的其他设备和Android版本上运行良好...我尝试过搜索,但我似乎无法找到真正解决此问题的任何内容......

我知道它是createScaledBitmap,因为如果我评论该行,一切正常 - 但我的位图不能缩放。

1 个答案:

答案 0 :(得分:0)

回答这个问题 -

真正的问题是,在那个特定的设备上,调整大小比例太大而不适合定义的655区域,图像稍微太小 - 这导致调整大小错误但是在运行4.0.3的HTC设备上抛出“无法创建SkBitmap”错误。

解决方案是调整已调整大小的图像的尺寸。