0

I have an map image and want to load the map using Picasso library. After the map loaded i want to draw canvas on it but I get this error.

java.lang.IllegalStateException: Immutable bitmap passed to Canvas constructor
at android.graphics.Canvas.<init>(Canvas.java:127)
at com.example.sev_user.smarthome.gui.fragment.MapFragment$1.onBitmapLoaded(MapFragment.java:108)
at com.squareup.picasso.TargetAction.complete(TargetAction.java:36)
at com.squareup.picasso.Picasso.deliverAction(Picasso.java:557)
at com.squareup.picasso.Picasso.complete(Picasso.java:509)
at com.squareup.picasso.Picasso$1.handleMessage(Picasso.java:116)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4950)
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:997)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:764)
at dalvik.system.NativeStart.main(Native Method)

Here is my code:

final Target target = new Target() {
        @Override
        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
            mMapCanvas = new Canvas(bitmap);
            mIvMap.setImageBitmap(bitmap);
            reloadMap();
        }

        @Override
        public void onBitmapFailed(Drawable errorDrawable) {

        }

        @Override
        public void onPrepareLoad(Drawable placeHolderDrawable) {

        }
    };
    mIvMap.setTag(target);
    Picasso.with(getActivity())
            .load(R.drawable.housemap)
            .config(Bitmap.Config.RGB_565)
            .into(target);

As I know canvas need a mutable bitmap but I don't want to use bitmap.copy(). It may relate to this post: Draw a line on ImageView set by Picasso but I need something doesn't create a new bitmap.

Please help me!

Community
  • 1
  • 1
thangdc94
  • 1,542
  • 2
  • 26
  • 38
  • Possible duplicate of [Draw a line on ImageView set by Picasso](http://stackoverflow.com/questions/27081534/draw-a-line-on-imageview-set-by-picasso) – Bö macht Blau Sep 27 '16 at 12:38
  • @0X0nosugar That post use bitmap.copy(). I don't want to make a new bitmap. – thangdc94 Sep 27 '16 at 12:42
  • All right, then please take a look at [this post](http://stackoverflow.com/a/9194259/5015207) about how to "convert your immutable bitmap to a mutable bitmap" - maybe it is more helpful although you will wind up creating a new Bitmap there as well. It only avoids having two of them in memory at the same time. – Bö macht Blau Sep 27 '16 at 12:55
  • I suspect that [Picasso will only ever give you immutable bitmaps](https://github.com/square/picasso/pull/317). AFAIK, the only way to make a mutable bitmap will involve a copy in some form. – CommonsWare Sep 27 '16 at 12:55
  • Thanks for the advice. – thangdc94 Sep 27 '16 at 13:10

0 Answers0