Android 异常:Immutable bitmap passed to Canvas constructor

First:

Get bitmap from your drawable resource:

Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.yourBitmap);

2nd way to get bitmap from drawable:

Drawable bitmapDrawable = getResources().getDrawable(R.drawable.ic_launcher);
Bitmap bm = ((BitmapDrawable) bitmapDrawable).getBitmap();


When using this bitmp with Canvas like this:

 Canvas canva = new Canvas(bm);

It throws:

java.lang.IllegalStateException

Immutable bitmap passed to Canvas constructor


Solution:

Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher).copy(Bitmap.Config.ARGB_8888, true);

你可能感兴趣的:(android)