将位图添加到Canvas时出现IllegalStateException

rah*_*hul 2 android canvas illegalstateexception

我试图将一个位图图像设置为画布使用 setBitMap,当时我得到了一个IllegalStateException.这个画布当前有一些图像,我试图替换它.任何人都知道为什么会这样?

代码片段

editBm = Bitmap.createBitmap(951, 552, Bitmap.Config.ARGB_8888);    
        Canvas mCanvas=new Canvas(editBm);
        eBit=LoadBMPsdcard(filePath); ---->returns a bitmap when the file path to the file is provided
        Log.i("BM size", editBm.getWidth()+"");
        mCanvas.setBitmap(eBit);
Run Code Online (Sandbox Code Playgroud)

我没有得到任何NullPointer错误,并且该方法 LoadBMPsdcard() 运行良好.

请告诉我你的任何想法......

提前致谢

快乐的编码

Zer*_*Tek 6

可能会抛出IllegalStateException,因为您正在加载Bitmap(eBit)并在mCanvas.setBitmap(eBit) 不检查位图是否可变的情况下使用.这需要在Bitmap上绘制.要确保您的Bitmap是可变的,请使用:

eBit=LoadBMPsdcard(filePath);
Bitmap bitmap = eBit.copy(Bitmap.Config.ARGB_8888, true);
canvas.setBitmap(bitmap);
Run Code Online (Sandbox Code Playgroud)