After searching a lot in all the related issues at Stack Overflow and finding nothing, please try to help me.
I created an intent for capture a picture. Then I saw different behavior at onActivityResult()
: if I don't put any extra in the Intent (for small pics) the Intent in onActivityResult is ok, but when I put extras in the intent for writing the pic to a file, the intent in onActivityResult is null
!
The Intent creation:
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// without the following line the intent is ok
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
startActivityForResult(takePictureIntent, actionCode);
Why is it null, and how can I solve it?
It happens the same to me, if you are providing
MediaStore.EXTRA_OUTPUT
, then the intent is null, but you will have the photo in the file you provided (Uri.fromFile(f)
).If you don't specify
MediaStore.EXTRA_OUTPUT
then you will have an intent which contains the uri from the file where the camera has saved the photo.Don't know if it as a bug, but it works that way.
EDIT: So in onActivityResult() you no longer need to check for data if null. The following worked with me:
Hope it helps
A sample written in Kotlin. You create a
Uri
for camera app,CameraFragment
holds it until camera returns from saving your picture and gives it back to you inonActivityResult
as you would expect.CameraFragment.kt
Acts as an intermediary between consumer and camera app. Takes
Uri
as input and returns it in dataIntent
.Invoke camera intent
this
is a fragment in your app which will trigger the camera.RC_CAMERA
is your request code for this action.Handle camera result
Where did you create the
f
for theUri.fromFile(f)
?It must be a valid
File
object. Try to create it before theEXTRA_OUTPUT
line.Try with something like this:
use the following:
Bitmap bitmap = data.getExtras().getParcelable("data");
it works.