使用BitmapFactory的decodeResource方法需注意,不能直接修改res里面的图片,需要添加copy(Bitmap.Config.ARGB_8888,true)方法,否则会报以下异常:
Caused by: java.lang.IllegalStateException: Immutable bitmap passed to Canvas constructor
代码如下:
private Bitmap getBitmap(Context context, int resId) {
BitmapFactory.Options options = new BitmapFactory.Options();
TypedValue value = new TypedValue();
context.getResources().openRawResource(resId,value);
options.inTargetDensity = value.density;
options.inScaled = false;
return BitmapFactory.decodeResource(context.getResources(),resId,options).copy(Bitmap.Config.ARGB_8888,true);
}
网友评论