首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >不可变位图崩溃错误

不可变位图崩溃错误
EN

Stack Overflow用户
提问于 2012-10-29 18:15:04
回答 4查看 58.8K关注 0票数 79
代码语言:javascript
运行
复制
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)

我从开发人员控制台得到这个崩溃错误。我不明白问题出在哪里。

代码语言:javascript
运行
复制
    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);

我想是和这个有关吧?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-10-29 18:25:41

你必须将你的workingBitmap转换成Mutable Bitmap以便在Canvas上绘图。(注:此方法无助于节省内存,它将使用额外的内存)

代码语言:javascript
运行
复制
Bitmap workingBitmap = Bitmap.createBitmap(chosenFrame);
Bitmap mutableBitmap = workingBitmap.copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(mutableBitmap);

此答案有助于避免浪费内存Convert immutable bitmap to a mutable bitmap

票数 218
EN

Stack Overflow用户

发布于 2013-10-29 06:22:20

BitmapFactory.decodeResource()返回位图的不可变副本,您不能绘制到它的画布上。为了得到它的画布,你需要得到图像的位图的一个可变副本,这可以通过添加单行代码来完成。

代码语言:javascript
运行
复制
opt.inMutable = true;

将这一行添加到您的代码中,它应该可以解决崩溃问题。

票数 41
EN

Stack Overflow用户

发布于 2019-11-23 00:01:08

这也行得通,我刚刚测试过了。

代码语言:javascript
运行
复制
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inMutable = true;
return BitmapFactory.decodeByteArray(resultDecoded, 0, resultDecoded.length,options);
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13119582

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档