android camera intent not working properly on some devices

3.9k views Asked by At

Hi I am developing android application in which I am trying to capture image using device camera and save it to particular location. I tried it in following manner:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Log.i("inside on create ", "inside on create ");

    capturePhotoForRecordUpload();
}

public void capturePhotoForRecordUpload() {

    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {

        File storageDir = new File(Environment.getExternalStorageDirectory().getPath() + "/MyApp");
        if(!storageDir.exists())
            storageDir.mkdirs();
        if(!storageDir.exists())
            storageDir.mkdir();

            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory().getPath() + "/MyApp"+"/sample.jpg")));
            startActivityForResult(takePictureIntent, 7);
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
    }

}
}

I am facing problem in above code. When I start my activity it is opening my camera. But when I click back button it again coming to on create of my activity and again opening my camera application. Is that correct behaviour or I am doing anything wrong? without calling on destroy of my activity it is again coming inside on create.Need some help. Thank you.

2

There are 2 answers

6
GreyBeardedGeek On

Its a bit unusual to launch a new activity before the launching activity is shown.

However, I suspect that things would work better if you did this later, perhaps in onResume().

You might still have a similar problem, though. You might have to keep track yourself of whether you're expecting a result, so that you don't launch the camera intent twice.

2
JanBo On

I had the same issue on few devices when using the camera. Put logs in your activity onDestory and see if it gets called when you start the camera intent.

This issue occurred on devices with low memory (for me it was an older LG phone that had 50+ apps installed with many of them running various background services that we used for testing) -> the camera app is memory heavy and thus the system would kill the process in background which started it. Thats why you see your activity going through its complete lifecycle again.

Edit 1:

Take a look at the following answers:

Android: Activity getting Destroyed after calling Camera Intent

Trouble working with the camera in onActivityResult

Activity gets killed after returned from the camera


For some people the solution was acquired through manifest changes (see first link)

I implemented the solutions described in other answers which was handling the savedInstanceState bundle since i was already controlling the image path manually.

Also you will see another reason why the activity would get recreated, because some camera apps on some phones trigger orientation changes. It could be your problem too, if its not a memory problem.