Reputation: 1139
I am trying to use the Activity Result APIs to handle the picking of a single photo for an app I am developing. I am trying to use one of the predefined contracts to keep things simple. So, I am attempting to use the ActivityResultContracts.PickVisualMedia() contract.
I am setting the Activity Result Launcher up as follows:
private ActivityResultLauncher<PickVisualMediaRequest> pickVisualMediaActivityResultLauncher;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
pickVisualMediaActivityResultLauncher = registerForActivityResult(
new ActivityResultContracts.PickVisualMedia(),
this::onPickVisualMediaActivityResult
);
}
And I am attempting to construct a PickVisualMediaRequest and launch the Activity Result Launcher here:
private void onSelectNewPhotoButtonClick() {
PickVisualMediaRequest request = new PickVisualMediaRequest.Builder()
.setMediaType(new ActivityResultContracts.PickVisualMedia.ImageOnly())
.build();
pickVisualMediaActivityResultLauncher.launch(request);
}
Issue is that Android Studio is complaining about ActivityResultContracts.PickVisualMedia.ImageOnly() not having proper visibility to be used, even though it is a valid VisualMediaType and the docs imply that it should be used this way:
I can't really find any code samples on this particular scenario. Am I missing something? Does the API have a visibility defect or am I just dumb today?
Upvotes: 11
Views: 11676
Reputation: 13
For me this code from vendor (last sample) is working (but yep, the error is still bothering so I'd like to find stable solution as well).
Upvotes: 0
Reputation: 338
Actually, I also got the same error but when I executed the code it was compiled and run and worked as expected.
you can refer to the code
pickMedia.launch(new PickVisualMediaRequest.Builder() .setMediaType(ActivityResultContracts.PickVisualMedia.ImageOnly.INSTANCE) .build());
visit to here
Upvotes: 0
Reputation: 1573
As of March 22, 2023. There is a fix just update androidx.activity:activity to version 1.7.0.
Upvotes: 3
Reputation: 1139
After some help from CommonsWare, I determined that setMediaType()
accepts a Kotlin object instance. So, the above bad function I had should be:
private void onSelectNewPhotoButtonClick() {
ActivityResultContracts.PickVisualMedia.VisualMediaType mediaType = (ActivityResultContracts.PickVisualMedia.VisualMediaType) ActivityResultContracts.PickVisualMedia.ImageOnly.INSTANCE;
PickVisualMediaRequest request = new PickVisualMediaRequest.Builder()
.setMediaType(mediaType)
.build();
pickVisualMediaActivityResultLauncher.launch(request);
}
Android Studio complains about the type casting, but the code does compile and work as expected. Very bizarre.
Upvotes: 15