Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When i try to get the Uri from a picture taken with the camera i get null

here is my problem, when i try to get the Uri from the picture that I take whit the camera i get null with the camera on emulator and my device, but only whit the system camera if I use another camera app, always works. Here is my code

For launch the camera app

takePic.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    String captured_image = System.currentTimeMillis() + ".jpg";
    File file = new File(Environment.getExternalStorageDirectory(), captured_image); 
    captured_image = file.getAbsolutePath();
    outputFileUri = Uri.fromFile(file); 
    intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); 
    intent.putExtra("return-data", true);
    startActivityForResult(intent, CAMERA_REQUEST);
   }
});

for get the image

protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  if (requestCode == CAMERA_REQUEST) { 
    ContentResolver cr = getContentResolver();
    InputStream in = null;
    try 
    {
        in = cr.openInputStream(outputFileUri); 
    } 
    catch (FileNotFoundException e) 
    {
        e.printStackTrace();
    }
  }
}

outputFileUri always have null when the activity returns. Thanks.

like image 255
ludwiguer Avatar asked Jan 28 '26 18:01

ludwiguer


1 Answers

Android might have killed off (and restarted) your activity before you get into onActivityResult, for instance because you rotated your device while taking the picture. Try to store and restore outputFileUri with the rest of the Activity state...

protected void onSaveInstanceState(Bundle outState)
{
    outState.putParcelable("outputFileUri", outputFileUri);
}

...

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    if (savedInstanceState != null)
    {
        outputFileUri= savedInstanceState.getParcelable("outputFileUri");
    }
}
like image 67
Paul-Jan Avatar answered Jan 30 '26 09:01

Paul-Jan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!