Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8677877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:34:34+00:00 2026-06-12T20:34:34+00:00

I have this code public void DetectFacesInImage(View view) { BitmapFactory.Options bitmapFactoryOptions=new BitmapFactory.Options(); bitmapFactoryOptions.inPreferredConfig= Bitmap.Config.RGB_565;

  • 0

I have this code

 public void DetectFacesInImage(View view)
    {
        BitmapFactory.Options bitmapFactoryOptions=new BitmapFactory.Options();
        bitmapFactoryOptions.inPreferredConfig= Bitmap.Config.RGB_565;
        //int resID = getResources().getIdentifier(picturePath , "drawable", getPackageName());

        myBitmap=BitmapFactory.decodeFile(picturePath,bitmapFactoryOptions);
        int width=myBitmap.getWidth();
        int height=myBitmap.getHeight();
        detectedFaces=new FaceDetector.Face[number_of_faces];
        FaceDetector faceDetector=new FaceDetector(width,height,number_of_faces);
        number_of_faces_detected = faceDetector.findFaces(myBitmap, detectedFaces);
        Canvas canvas = new Canvas(myBitmap);// line 70 the point where it crashes
        Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);
        //canvas.drawBitmap(overlay, 0, 0, paint);
        paint.setColor(Color.GREEN);
        paint.setStyle(Paint.Style.STROKE); 
        paint.setStrokeWidth(3);
        PointF midPoint=new PointF();

        for(int count=0;count<number_of_faces_detected;count++)
        {
            Face face=detectedFaces[count];

            face.getMidPoint(midPoint);

            float eyeDistance=face.eyesDistance();
            canvas.drawRect(midPoint.x-eyeDistance, midPoint.y-eyeDistance, midPoint.x+eyeDistance, midPoint.y+eyeDistance, paint);
        }


    }

I want to draw the rectangle on the top of the bitmap. The bitmap is received through intent is is used like this

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detect_faces);
       // getActionBar().setDisplayHomeAsUpEnabled(true);
        Intent intent = getIntent();
        ImageView imageView = (ImageView) findViewById(R.id.imgView);
        picturePath = intent.getStringExtra(GetFaceActivity.PICTURE_PATH);
        imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
        //imageView.setOnTouchListener(this);

    }

but the code is crashing with error Immutable bitmap passed to Canvas constructor. What am i doing wrong here ?

Here is the stack trace

10-14 20:01:34.763: E/AndroidRuntime(31137): FATAL EXCEPTION: main
10-14 20:01:34.763: E/AndroidRuntime(31137): java.lang.IllegalStateException: Could not execute method of the activity
10-14 20:01:34.763: E/AndroidRuntime(31137):    at android.view.View$1.onClick(View.java:3098)
10-14 20:01:34.763: E/AndroidRuntime(31137):    at android.view.View.performClick(View.java:3620)
10-14 20:01:34.763: E/AndroidRuntime(31137):    at android.view.View$PerformClick.run(View.java:14322)
10-14 20:01:34.763: E/AndroidRuntime(31137):    at android.os.Handler.handleCallback(Handler.java:605)
10-14 20:01:34.763: E/AndroidRuntime(31137):    at android.os.Handler.dispatchMessage(Handler.java:92)
10-14 20:01:34.763: E/AndroidRuntime(31137):    at android.os.Looper.loop(Looper.java:137)
10-14 20:01:34.763: E/AndroidRuntime(31137):    at android.app.ActivityThread.main(ActivityThread.java:4507)
10-14 20:01:34.763: E/AndroidRuntime(31137):    at java.lang.reflect.Method.invokeNative(Native Method)
10-14 20:01:34.763: E/AndroidRuntime(31137):    at java.lang.reflect.Method.invoke(Method.java:511)
10-14 20:01:34.763: E/AndroidRuntime(31137):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:978)
10-14 20:01:34.763: E/AndroidRuntime(31137):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)
10-14 20:01:34.763: E/AndroidRuntime(31137):    at dalvik.system.NativeStart.main(Native Method)
10-14 20:01:34.763: E/AndroidRuntime(31137): Caused by: java.lang.reflect.InvocationTargetException
10-14 20:01:34.763: E/AndroidRuntime(31137):    at java.lang.reflect.Method.invokeNative(Native Method)
10-14 20:01:34.763: E/AndroidRuntime(31137):    at java.lang.reflect.Method.invoke(Method.java:511)
10-14 20:01:34.763: E/AndroidRuntime(31137):    at android.view.View$1.onClick(View.java:3093)
10-14 20:01:34.763: E/AndroidRuntime(31137):    ... 11 more
10-14 20:01:34.763: E/AndroidRuntime(31137): Caused by: java.lang.IllegalStateException: Immutable bitmap passed to Canvas constructor
10-14 20:01:34.763: E/AndroidRuntime(31137):    at android.graphics.Canvas.<init>(Canvas.java:133)
10-14 20:01:34.763: E/AndroidRuntime(31137):    at com.example.face_tag_upload.DetectFaces.DetectFacesInImage(DetectFaces.java:70)
10-14 20:01:34.763: E/AndroidRuntime(31137):    ... 14 more
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T20:34:36+00:00Added an answer on June 12, 2026 at 8:34 pm

    If you are working with API Level 11 and up then you need to specify inMutable as part of your BitMapFactory.Options, so;

    BitmapFactory.Option bitmapFactoryOptions = new BitmapFactory.Options();
    bitmapFactoryOptions.inPreferredConfig = Bitmap.Config.RGB_565;
    bitmapFactoryOption.inMutable = true;
    

    If you want to go earlier than API Level 11 then you will want to make a mutable bitmap using the following snippet;

    Bitmap immutableBitmap = BitmapFactory.decodeFile(picturePath, bitmapFactoryOptions);
    int left = 0;
    int top = 0;
    int width = immutableBitmap.getWidth();
    int height = immutableBitmap.getHeight();
    boolean hasAlpha = immutableBitmap.hasAlpha();
    Bitmap mutableBitmap = immutableBitmap.createBitmap(width, height, hasAlpha);
    Canvas c = new Canvas();
    c.setDevice(mutableBitmap);
    c.drawBitmap(immutableBitmap, left, top, paint);
    

    The paint variable comes from your pre-existing code.

    This answer writes the original bitmap to disk and then creates a mutable version of the bitmap, avoiding the need to have two bitmaps in memory.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code: hubSpinner.setOnItemSelectedListener(new OnItemSelectedListener() { public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int
I have this code: list.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int
I have this code: @Override public void onSurfaceCreated(GL10 gl, EGLConfig config) { GLES20.glClearColor(0, 0,
I have this code for an event handler: rbM.setOnCheckedChangeListener(new OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton
Context I have this piece of Java Code btn.setOnAction(new EventHandler<ActionEvent>() { public void handle(ActionEvent
EDIT I now have this code: public void sendMessage(View view) { Intent intent =
I have this code: public void readTroops() { File file = new File(resources/objects/troops.txt); StringBuffer
I have this simple code: public void Run() { var invokerThread = new Thread(new
Lets say I have this code: public void MyMethod(string Data, List<string> InputData) { //I
i have this code public interface Interfcae1 { void OP1(); } public interface Interfcae2

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.