0

This is weird. The following code was working a few weeks ago, and now it is not:

private void onClick()
{
   Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
   mOriginalUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),"original" + String.valueOf(System.currentTimeMillis()) + ".jpg"));
   Log.d("path", mOriginalUri.getPath());
   intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mOriginalUri);
   intent.putExtra("return-data", true);
   startActivityForResult(intent, CAMERA_REQUEST);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) 
{
   switch( requestCode )
   {
   case CAMERA_REQUEST:
       if(resultCode == MainActivity.RESULT_OK)
          Log.d("path", mOriginalUri.getPath());
       break;
   }
}

Somehow the camera capture activity is losing track of my Uri and setting it to null. I get a NPE when printing the path in the onActivityResult. How can I resolve this? I'm using Android 4.1.2 API 16 on a Galaxy Nexus.

Michael Eilers Smith
  • 8,466
  • 20
  • 71
  • 106

1 Answers1

0

I had a similar issue of the camera sometimes returning the image and sometimes returning null. I had the same setup of writing the image to a file.

I kicked off the camera intent from onCreate(), not an onClick() method.

When I locked the screen orientation of the intent (to portrait, but that does not matter), the problem went away. May want to try that and report back if that made a difference.

This may also be relevant: Camera activity causing uri to go to null when screen orientation changes

Community
  • 1
  • 1
Istvan Jonyer
  • 151
  • 1
  • 9