Search code examples
javaandroidandroid-camera-intent

Camera Intent call onDestroy()


In my app I want to click photo from camera and want to get content URI from camera result in onActivityResult() method.

In my App I am getting content URL using below method.

Uri currImageURI;
ArrayList<Uri> imageItems = new ArrayList<Uri>();
    @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            // TODO Auto-generated method stub
            if((requestCode == SELECT_PICTURE_FROM_CAMERA && resultCode == RESULT_OK) || (requestCode == SELECT_PICTURE_FROM_LIB && resultCode == RESULT_OK)) {
                currImageURI = data.getData();
                imageItems.add(currImageURI);
                Toast.makeText(UpdateMyServiceActivity.this, "Image added.", Toast.LENGTH_LONG).show();
            }
        }

I added uri in ArrayList (Because user can upload multiple images).

There is save button in my app.

When user click on save button, I pass imageItems to AsyncTask for upload image to server.

But here is problem.

When user click on save button, I am getting imageItems null.

I debug my app. After add URI to ArrayList, below Activity methods are called.

onResume()
onPause()
onStop()
onDestroy()
onCreate()
onStart()
onResume()

That's why I am getting arrayList null.

How to solve this issue?


Solution

  • currImageURI = data.getData();
    

    If you are using ACTION_IMAGE_CAPTURE, this is incorrect code. ACTION_IMAGE_CAPTURE does not return a Uri, though a small number of camera apps will. Instead, you supply EXTRA_OUTPUT on the ACTION_IMAGE_CAPTURE Intent itself. Then, you hold onto whatever value you used with EXTRA_OUTPUT, and you look there for your image in onActivityResult().

    That's why I am getting arrayList null.

    Your activity is being destroyed and recreated, probably because your process was terminated while the camera app was in the foreground. This is perfectly normal. Your process can be terminated any time your app is not in the foreground.

    How to solve this issue?

    Hold onto the ArrayList via the saved instance state Bundle.