Nice programing

변경 불가능한 비트 맵 충돌 오류

nicepro 2020. 11. 12. 20:12
반응형

변경 불가능한 비트 맵 충돌 오류


java.lang.IllegalStateException: Immutable bitmap passed to Canvas constructor
at android.graphics.Canvas.<init>(Canvas.java:127)
at app.test.canvas.StartActivity.applyFrame(StartActivity.java:214)
at app.test.canvas.StartActivity$1.onClick(StartActivity.java:163)
at android.view.View.performClick(View.java:4223)
at android.view.View$PerformClick.run(View.java:17275)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4898)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
at dalvik.system.NativeStart.main(Native Method)

개발자 콘솔에서이 충돌 오류가 발생합니다. .. 문제가 무엇인지 이해할 수 없습니다 ..

    BitmapFactory.Options opt = new BitmapFactory.Options();
    opt.inScaled = true;
    opt.inPurgeable = true;
    opt.inInputShareable = true;
    Bitmap brightBitmap = BitmapFactory.decodeResource(getResources(), position, opt); 
    brightBitmap = Bitmap.createScaledBitmap(brightBitmap, 550, 550, false);
    chosenFrame = brightBitmap;
    Bitmap workingBitmap = Bitmap.createBitmap(chosenFrame);
    workingBitmap = Bitmap.createBitmap(workingBitmap); 
    Canvas c = new Canvas(workingBitmap);

이것과 관련된 것 같아요?


에서 그리기 위해 를로 변환해야 workingBitmap합니다 . (참고 :이 방법은 메모리를 절약하는 데 도움이되지 않으며 추가 메모리를 사용합니다.)Mutable BitmapCanvas

Bitmap workingBitmap = Bitmap.createBitmap(chosenFrame);
Bitmap mutableBitmap = workingBitmap.copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(mutableBitmap);

이 답변은 메모리를 낭비하지 않는 데 도움이 됩니다. 변경 불가능한 비트 맵을 변경 가능한 비트 맵으로 변환


BitmapFactory.decodeResource()비트 맵의 ​​변경 불가능한 복사본을 반환하며 캔버스에 그릴 수 없습니다. 캔버스를 얻으려면 이미지 비트 맵의 ​​변경 가능한 사본을 가져와야하며 이는 한 줄 코드 추가로 수행 할 수 있습니다.

opt.inMutable = true;

코드에 해당 줄을 추가하면 충돌을 해결해야합니다.


IMMUTABLE 비트 맵을 MUTABLE 비트 맵으로 만들고 싶지 않다면 항상 BITMAP을 재사용 하여 메모리를 절약 할 수 있습니다.

workingBitmap = workingBitmap.copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(workingBitmap);

그러나 이것은 호출하여 비트 맵을 변경 가능하게 만드는 것과 같을 수 있다고 생각합니다.

workingBitmap.isMutable = true

메모리 사용량을 최소화하기 위해 리소스에서 직접 변경 가능한 비트 맵을 변환 / 디코딩하는 방법에 대한이 게시물을 확인할 수 있습니다.

https://stackoverflow.com/a/16314940/878126

참고 URL : https://stackoverflow.com/questions/13119582/immutable-bitmap-crash-error

반응형