0

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.

nilkash
  • 7,408
  • 32
  • 99
  • 176
  • Means You need to close the App when click back button ? – Rajan Bhavsar Jun 19 '15 at 12:10
  • Yeah either close or previous activity. But What happened I am opening camera inside on create of activity. So when I click on back button from camera it comes to above activity. It is again going to oncreate of that activity and opening camera again. – nilkash Jun 19 '15 at 12:11
  • No i used your code but on back pressed i have black screen instead of calling the onCreate again. – Rajan Bhavsar Jun 19 '15 at 12:14
  • @RajanBhavsar it working fine on some devices. But on some devices it is causing this issue. – nilkash Jun 19 '15 at 12:21
  • unconditional staring new Activity in onCreate make no sens – Selvin Jun 19 '15 at 12:23
  • Please look at answers to a [related question](http://stackoverflow.com/questions/20424909/android-startcamera-gives-me-null-intent-and-does-it-destroy-my-global-varia). You will find some best practices for handling similar situations. – Alex Cohn Jun 19 '15 at 16:00

2 Answers2

5

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.

Community
  • 1
  • 1
JanBo
  • 2,925
  • 3
  • 23
  • 32
1

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.

GreyBeardedGeek
  • 29,460
  • 2
  • 47
  • 67
  • Yeah that true, but my concern is why it is again starting activity from on create? – nilkash Jun 19 '15 at 12:13
  • 1
    Possibly because you send the intent before oncreate finishes. But Android can always kill your Activity while its in the background (which it is while the camera app is running) and then re-create it when you come to the foreground. That's just how it works. – GreyBeardedGeek Jun 19 '15 at 12:16
  • But If that is the case then it should also call onDestroy also in that case. But it is not doing that as well. And if I put open camera call inside onresume then it will open camera on every onresume of activity. – nilkash Jun 19 '15 at 12:20
  • Also it is not even consistent. – nilkash Jun 19 '15 at 12:23
  • 1
    yes, it is ... do not trust onDestroy, ever ... your code is not consistent ... actually `onResume` is a good place( **for conditional launch** ) as it should be called after `onActivityResult` – Selvin Jun 19 '15 at 12:25
  • It is starting your Acitivty from onCreate because that is the regular lifecycle of your activity after it is killed....check tour saved instance state in such cases. See this post for more details: http://stackoverflow.com/questions/26359130/why-oncreate-method-called-after-startactivityforresult – JanBo Jun 20 '15 at 20:38