In androidx.activity version 1.2.0-alpha05 API for TakePicture contract has been changed:
The TakePicture contract now returns a boolean indicating success rather than a thumbnail Bitmap as this was very rarely supported by camera apps when writing the image to the provided Uri
While in alpha04 callback received a Bitmap object, now only a Boolean object that describes success is received by the callback.
So now the Uri Parameter of the launch method of the launcher must not be null, but must be the destination where the picture is saved. Did not manage to create an Uri object that is accepted by the launcher and that can be used for my app to read the result picture.
Does anybody have an example for me for a valid Uri object that can be provided to the launcher?
In simple words, if we have to start an activity for result using ActivityResultAPI then we will simply use ActivityResultLauncher like capturePhoto.invoke (). This will launch ActivityResultContract which in turn launch the intent to startTheActivity by itself.
The newly taken picture will be displayed inside the ImageView element. Anytime we take a new picture, the ImageView will refresh its current Drawable. Before we can implement the ActivityResult API, we need to agree on a way to save the taken pictures first.
ActivityResultLauncher: A launcher that is used to start the process of executing an ActivityResultContract. capturePhoto is nothing but our ActivityResultLauncher which is returned by registerForActivityResult ().
ActivityResultContracts.TakePicture () is one of the built-in helpers which Google have created for us, and finally invoking takePicture actually triggers the Intent in the same way that you would previously with Activity.startActivityForResult (intent, REQUEST_CODE). The built-in ActivityResultContracts currently include a few common operations:
I can't find any example on the internet
Here is an example.
File file = new File(getFilesDir(), "picFromCamera");
Uri uri = FileProvider.getUriForFile(this, getApplicationContext().getPackageName() + ".provider", file);
ActivityResultLauncher<Uri> mGetContent = registerForActivityResult(
new ActivityResultContracts.TakePicture(),
new ActivityResultCallback<Boolean>() {
@Override
public void onActivityResult(Boolean result) {
// do what you need with the uri here ...
}
});
mGetContent.launch(uri);
Note1: You are likely to run into FileUriExposedException
, need to expose this uri for the camera app to access
Related: android.os.FileUriExposedException: file:///storage/emulated/0/test.txt exposed beyond app through Intent.getData()
Note2: If you have <uses-permission android:name="android.permission.CAMERA" />
declared in your manifest, you need to have permission before launching, otherwise java.lang.SecurityException: Permission Denial
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With