Search code examples
androidimagebitmapandroid-cameraandroid-canvas

setting an immutable bitmap image gives errors?


i'm learning about graphics.Camera. and i have taken a picture by the camera and when i set

the taken picture "bitmap image" as canvas, the App crashs and the logcat says "the bitmap

image is immutable". actually, i dont know what does that mean? Any suggestion?

thanks in advance.

Code

OnClickListener btn01Listener = new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Intent CameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(CameraIntent, CAMERA_PIC_REQUEST);           
    }
};

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == CAMERA_PIC_REQUEST && resultCode == RESULT_OK) {
           image = (Bitmap) data.getExtras().get("data");
           ImageView img01 = (ImageView) findViewById(R.id.img01);
           //img01.setImageBitmap(image);

           c = new Camera();
           can = new Canvas();
           can.setBitmap(Bitmap.createBitmap(image));
        }
}

LogCaat:

03-23 20:46:16.188: E/AndroidRuntime(452): FATAL EXCEPTION: main

03-23 20:46:16.188: E/AndroidRuntime(452): java.lang.RuntimeException: Unable to resume activity {com.androidbook.Camera3DDemo/com.androidbook.Camera3DDemo.Camera3DDemoActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.androidbook.Camera3DDemo/com.androidbook.Camera3DDemo.Camera3DDemoActivity}: java.lang.IllegalStateException 03-23 20:46:16.188: E/AndroidRuntime(452): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2120) 03-23 20:46:16.188: E/AndroidRuntime(452): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2135) 03-23 20:46:16.188: E/AndroidRuntime(452): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1668) 03-23 20:46:16.188: E/AndroidRuntime(452): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:2832) 03-23 20:46:16.188: E/AndroidRuntime(452): at android.app.ActivityThread.access$1600(ActivityThread.java:117) 03-23 20:46:16.188: E/AndroidRuntime(452): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935) 03-23 20:46:16.188: E/AndroidRuntime(452): at android.os.Handler.dispatchMessage(Handler.java:99) 03-23 20:46:16.188: E/AndroidRuntime(452): at android.os.Looper.loop(Looper.java:130) 03-23 20:46:16.188: E/AndroidRuntime(452): at android.app.ActivityThread.main(ActivityThread.java:3683) 03-23 20:46:16.188: E/AndroidRuntime(452): at java.lang.reflect.Method.invokeNative(Native Method) 03-23 20:46:16.188: E/AndroidRuntime(452): at java.lang.reflect.Method.invoke(Method.java:507) 03-23 20:46:16.188: E/AndroidRuntime(452): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 03-23 20:46:16.188: E/AndroidRuntime(452): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 03-23 20:46:16.188: E/AndroidRuntime(452): at dalvik.system.NativeStart.main(Native Method) 03-23 20:46:16.188: E/AndroidRuntime(452): Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.androidbook.Camera3DDemo/com.androidbook.Camera3DDemo.Camera3DDemoActivity}: java.lang.IllegalStateException 03-23 20:46:16.188: E/AndroidRuntime(452): at android.app.ActivityThread.deliverResults(ActivityThread.java:2532) 03-23 20:46:16.188: E/AndroidRuntime(452): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2107) 03-23 20:46:16.188: E/AndroidRuntime(452): ... 13 more 03-23 20:46:16.188: E/AndroidRuntime(452): Caused by: java.lang.IllegalStateException 03-23 20:46:16.188: E/AndroidRuntime(452): at android.graphics.Canvas.setBitmap(Canvas.java:144) 03-23 20:46:16.188: E/AndroidRuntime(452): at com.androidbook.Camera3DDemo.Camera3DDemoActivity.onActivityResult(Camera3DDemoActivity.java:43) 03-23 20:46:16.188: E/AndroidRuntime(452): at android.app.Activity.dispatchActivityResult(Activity.java:3908) 03-23 20:46:16.188: E/AndroidRuntime(452): at android.app.ActivityThread.deliverResults(ActivityThread.java:2528) 03-23 20:46:16.188: E/AndroidRuntime(452): ... 14 more


Solution

  • Actually that means that you don't have the permissions to change the Bitmap. So you will have to create a mutable copy of your provided bitmap which can be used to paint on a Canvas and such.

    You can do that by using this method:

    Bitmap.createBitmap(yourBitmap);