I want to take a picture with the camera and then send it to another activity. Everything works on the emulator without any problems. But as soon as I try the app on my real device, I always get the status code 0 in the ActivityResultLauncher and the If branch is not called. On the other hand, on the emulator I get -1 and the intent for calling the second activity started. What am I doing wrong ?
I use Android 12 (SKD 31) on real device
I have now also tested it with an emulator skd > 29 -> same problem. As soon as I press the Ok button in the Camera View for an image, Android throws me back to the MainActivity and not, as expected and desired, to the AddNewPlace activity. No error in debuger/logcat. As soon as I use a version SDK < 29 everything works without problems. I think it has something to do with the FileProvide or with the EXTRA_OUTPUT in the intent.
Manifest.xml
This one, also won't work
The code for the camera:
File file = new File(Environment.getExternalStorageDirectory(),
UUID.randomUUID()+".jpg");
outputFileUri = FileProvider.getUriForFile(MainActivity.this,BuildConfig.APPLICATION_ID + ".fileprovider",file);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityIntent.launch(intent);
ActivityResultLauncher<Intent> startActivityIntent = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
if(result.getResultCode() == RESULT_OK) // 0 on real device , -1 on Emulator
{
if(result.getData() != null)
{
Log.e("FILE : ", outputFileUri.toString());
Intent intent = new Intent(MainActivity.this,AddNewPlace.class);
intent.putExtra("File",outputFileUri.toString());
startActivity(intent);
}
}
}
});
EDIT
Updated code, doesn't work either. Do I have to change something in the FileProvider?
File file =
MainActivity.this.getExternalFilesDir(UUID.randomUUID()+".jpg");
File file = new File(Environment.getExternalStorageDirectory(), UUID.randomUUID()+".jpg");
On Android 11+ devices you cannot create files in root of external storage. Use a public directory on external storage instead. – Tasteful