0

In my app, I am using an intent to start the phone's camera, take a picture and save it locally on the phone. This is all working fine, however, once I press save (to save the picture) the the app quits and it takes me back to the home screen of my phone. I'm not sure what I'm missing so that after I press save, it goes back to my app rather than the home screen? Below is my code:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    //other code, removed as its unrelated      

    btnAddExpense.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            //Toast.makeText(context, "List Item Clicked.", Toast.LENGTH_LONG).show();

            // create intent with ACTION_IMAGE_CAPTURE action 
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

            // this part to save captured image on provided path
            File file = new File(Environment.getExternalStorageDirectory(), "AutoTracker_" + System.currentTimeMillis() + ".jpg");
            Uri photoPath = Uri.fromFile(file);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, photoPath);

            // start camera activity
            startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
        }
    });    
        return rowView;          
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
      if (resultCode == Activity.RESULT_OK) {
          // Image captured and saved to fileUri specified in the Intent
          Toast.makeText(context, "Image saved to:\n" + data.getData(), Toast.LENGTH_LONG).show();
      } else if (resultCode == Activity.RESULT_CANCELED) {
          // User cancelled the image capture
      } else {
          // Image capture failed, advise user
      }
    }

    if (requestCode == CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE) {
       if (resultCode == Activity.RESULT_OK) {
           // Video captured and saved to fileUri specified in the Intent
           Toast.makeText(context, "Video saved to:\n" + data.getData(), Toast.LENGTH_LONG).show();
       } else if (resultCode == Activity.RESULT_CANCELED) {
           // User cancelled the video capture
       } else {
           // Video capture failed, advise user
       }
    }
}

Thanks!

jww
  • 97,681
  • 90
  • 411
  • 885
user2573690
  • 5,493
  • 9
  • 43
  • 61
  • The behavior of the camera app is up to the camera app (of which there are hundreds, if not thousands), which makes these `Intent` actions rather unreliable. – CommonsWare Dec 08 '14 at 00:16
  • I'm not sure what that means...should I not be using an intent? In the documentation that's how it starts out. My app is not primarily a camera app but this is just a small part of it – user2573690 Dec 08 '14 at 00:23
  • Yeah, I would hazard a guess that `data` is null. http://stackoverflow.com/questions/1910608/android-action-image-capture-intent – tachyonflux Dec 08 '14 at 00:23
  • "I'm not sure what that means" -- it means that the behavior of the camera app is up to the camera app. The camera app does not have to have a save button. If it does, what happens when the user presses the save button is up to the developers of the camera app. They can return to you, or go home, or return to another activity in the camera app, or launch a random app, or whatever they feel like. "In the documentation that's how it starts out" -- Google has overly optimistic expectations of what camera app developers will do. – CommonsWare Dec 08 '14 at 00:26
  • I would focus first on determining if you are somehow crashing (e.g., look for a stack trace in LogCat) and determine if you are getting your `onActivityResult()` called (e.g., use actual `Log` statements, in addition to or instead of `Toasts`). But if you are expecting that every camera app ever written will always return to your app after you try to take a picture using their app, prepare to be disappointed. – CommonsWare Dec 08 '14 at 00:28
  • Thanks! I now understand what you mean, and it doesn't look like it is even getting to the onActivityResut(). Now another question, where in this camera app that starts when you create the intent would I add the behaviour for the OK button? – user2573690 Dec 08 '14 at 00:50

0 Answers0