所以我有一个使用 decodeFile 加载图像的函数 - 然后我正在调整照片的大小并将其绘制到画布中。这在我的 Moto Droid 2.2.3 和我的 Nexus 4.1.1 上非常有效 - 但在我的 HTC 4.0.3 上,每次 createScaledBitmap 触发时它都会崩溃并抛出错误“无法创建 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,因为如果我评论那条线一切正常 - 但我的位图无法缩放。