Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActivityResultContracts.TakePicture()

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?

like image 264
Werner Harnisch Avatar asked May 21 '20 19:05

Werner Harnisch


People also ask

How to start an activity for result using activityresultapi?

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.

Where will the newly taken picture be displayed in the activityresult?

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.

What is the use of activityresultlauncher?

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 ().

What is the use of takepicture() method in Android?

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:


1 Answers

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

like image 108
BabyishTank Avatar answered Oct 26 '22 22:10

BabyishTank