Skip to content

Commit

Permalink
image_picker: don't require QUERY_ALL_PACKAGES on sdk30
Browse files Browse the repository at this point in the history
resolveActivity(intent) might return false on android 11+_even though startActivity(intent) would succeed.
Instead of introducing a <queries> entry we can just try to start the
activity and catch the exception if that doesn't work.

Properly fixes flutter/flutter#62669.

Ref: https://cketti.de/2020/09/03/avoid-intent-resolveactivity/
  • Loading branch information
Bubu committed Jan 3, 2021
1 parent cfa7098 commit 80f2df7
Showing 1 changed file with 13 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import android.Manifest;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
Expand Down Expand Up @@ -299,21 +300,19 @@ private void launchTakeVideoWithCameraIntent() {
useFrontCamera(intent);
}

boolean canTakePhotos = intentResolver.resolveActivity(intent);

if (!canTakePhotos) {
finishWithError("no_available_camera", "No cameras available for taking pictures.");
return;
}

File videoFile = createTemporaryWritableVideoFile();
pendingCameraMediaUri = Uri.parse("file:" + videoFile.getAbsolutePath());

Uri videoUri = fileUriResolver.resolveFileProviderUriForFile(fileProviderName, videoFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, videoUri);
grantUriPermissions(intent, videoUri);

activity.startActivityForResult(intent, REQUEST_CODE_TAKE_VIDEO_WITH_CAMERA);
try {
activity.startActivityForResult(intent, REQUEST_CODE_TAKE_VIDEO_WITH_CAMERA);
} catch (ActivityNotFoundException e) {
finishWithError("no_available_camera", "No cameras available for taking pictures.");
return;
}
}

public void chooseImageFromGallery(MethodCall methodCall, MethodChannel.Result result) {
Expand Down Expand Up @@ -366,21 +365,19 @@ private void launchTakeImageWithCameraIntent() {
useFrontCamera(intent);
}

boolean canTakePhotos = intentResolver.resolveActivity(intent);

if (!canTakePhotos) {
finishWithError("no_available_camera", "No cameras available for taking pictures.");
return;
}

File imageFile = createTemporaryWritableImageFile();
pendingCameraMediaUri = Uri.parse("file:" + imageFile.getAbsolutePath());

Uri imageUri = fileUriResolver.resolveFileProviderUriForFile(fileProviderName, imageFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
grantUriPermissions(intent, imageUri);

activity.startActivityForResult(intent, REQUEST_CODE_TAKE_IMAGE_WITH_CAMERA);
try {
activity.startActivityForResult(intent, REQUEST_CODE_TAKE_IMAGE_WITH_CAMERA);
} catch (ActivityNotFoundException e) {
finishWithError("no_available_camera", "No cameras available for taking pictures.");
return;
}
}

private File createTemporaryWritableImageFile() {
Expand Down

0 comments on commit 80f2df7

Please sign in to comment.