8

I'm trying to draw on a user loaded bitmap. This is in onCreate.

imageView = (ImageView) this.findViewById(R.id.ImageView); Display currentDisplay = getWindowManager().getDefaultDisplay(); float dw = currentDisplay.getWidth(); float dh = currentDisplay.getHeight(); bitmap = Bitmap.createBitmap((int) dw, (int) dh, Bitmap.Config.ARGB_8888); canvas = new Canvas(bitmap); paint = new Paint(); paint.setColor(Color.BLUE); imageView.setImageBitmap(bitmap); imageView.setOnTouchListener(this); 

Next code is when the user press a button to get the bitmap and after the bitmap is successfully retrieved. It is at the end of "onActivityResult"

canvas = new Canvas(bitmap); paint = new Paint(); paint.setColor(Color.BLUE); imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath)); imageView.setOnTouchListener(this); 

I'm able to draw on the blank bitmap that is made during onCreate, but when the user loads the new bitmap and try to draw it doesn't show. The new bitmap is loaded though.

I tired doing this too

canvas = new Canvas(BitmapFactory.decodeFile(picturePath)); 

then it gave me error.

Anyone know if i'm doing something wrong?

EDIT:THis is the log file:

10-25 03:19:31.409: W/System.err(1971): java.lang.Exception: java.lang.IllegalStateException: Immutable bitmap passed to Canvas constructor 10-25 03:19:31.409: W/System.err(1971): at com.example.milestone2.Draw.onActivityResult(Draw.java:148) 10-25 03:19:31.409: W/System.err(1971): at android.app.Activity.dispatchActivityResult(Activity.java:5293) 10-25 03:19:31.409: W/System.err(1971): at android.app.ActivityThread.deliverResults(ActivityThread.java:3315) 10-25 03:19:31.409: W/System.err(1971): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3362) 10-25 03:19:31.409: W/System.err(1971): at android.app.ActivityThread.access$1100(ActivityThread.java:141) 10-25 03:19:31.419: W/System.err(1971): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282) 10-25 03:19:31.419: W/System.err(1971): at android.os.Handler.dispatchMessage(Handler.java:99) 10-25 03:19:31.419: W/System.err(1971): at android.os.Looper.loop(Looper.java:137) 10-25 03:19:31.428: W/System.err(1971): at android.app.ActivityThread.main(ActivityThread.java:5041) 10-25 03:19:31.428: W/System.err(1971): at java.lang.reflect.Method.invokeNative(Native Method) 10-25 03:19:31.428: W/System.err(1971): at java.lang.reflect.Method.invoke(Method.java:511) 10-25 03:19:31.428: W/System.err(1971): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 10-25 03:19:31.428: W/System.err(1971): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 10-25 03:19:31.428: W/System.err(1971): at dalvik.system.NativeStart.main(Native Method) 10-25 03:19:31.438: W/System.err(1971): Caused by: java.lang.IllegalStateException: Immutable bitmap passed to Canvas constructor 10-25 03:19:31.448: W/System.err(1971): at android.graphics.Canvas.<init>(Canvas.java:127) 10-25 03:19:31.469: W/System.err(1971): at com.example.milestone2.Draw.onActivityResult(Draw.java:141) 10-25 03:19:31.469: W/System.err(1971): ... 13 more 10-25 03:19:31.588: E/SurfaceFlinger(37): ro.sf.lcd_density must be defined as a build property 
4
  • Can you see the bitmap in the imageView after you get the result? What error do you get? Commented Oct 25, 2013 at 2:53
  • Yep, it loads. I just can't draw on it. There's no crash. But when i tired canvas = new Canvas(BitmapFactory.decodeFile(picturePath)); I get this Commented Oct 25, 2013 at 3:01
  • com.example.milestone2.Draw.onActivityResult(Draw.java:139) 10-25 02:59:05.829: E/AndroidRuntime(1826): at android.app.Activity.dispatchActivityResult(Activity.java:5293) 10-25 02:59:05.829: E/AndroidRuntime(1826): at android.app.ActivityThread.deliverResults(ActivityThread.java:3315) 10-25 02:59:05.829: E/AndroidRuntime(1826): ... 11 more Commented Oct 25, 2013 at 3:01
  • Sorry but I would need to see the whole log. Update your question and post the whole of it.. I need to see if it is OutOfMem or NPE or what.. Commented Oct 25, 2013 at 3:05

1 Answer 1

21

You need to set the bitmap that is returned as the bitmap for the canvas. But BitmapFactory.decodeFile(picturePath) returns an immutable bitmap. Convert this to a mutable bitmap and then set is to the canvas.

Try this:

Bitmap loadedBitmap = BitmapFactory.decodeFile(picturePath); Bitmap drawableBitmap = loadedBitmap.copy(Bitmap.Config.ARGB_8888, true); canvas = new Canvas(drawableBitmap); paint = new Paint(); paint.setColor(Color.BLUE); imageView.setImageBitmap(drawableBitmap); imageView.setOnTouchListener(this); 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks it works I was barely able to see it, it's drawing on the top right hand corner. I'm not sure why but my drawing on the picture are confined to the top left and corner of the photo.
I'll try this Bitmap loadedBitmap = BitmapFactory.decodeFile(picturePath); Bitmap scaledloadedBitmap = Bitmap.createScaledBitmap(loadedBitmap, 120, 120, false); Bitmap drawableBitmap = scaledloadedBitmap.copy(Bitmap.Config.ARGB_8888, true);

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.