How to construct PickVisualMediaRequest for ActivityResultLauncher
Asked Answered
A

4

9

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: enter image description here

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?

Acalia answered 8/10, 2022 at 18:31 Comment(2)
ActivityResultContracts.PickVisualMedia.ImageOnly is a Kotlin object, not a class. In Kotlin, you would refer to that object simply as ActivityResultContracts.PickVisualMedia.ImageOnly. I forget how the JVM interop works for these. Regardless, you do not create an instance of ActivityResultContracts.PickVisualMedia.ImageOnly, but use the pre-existing singleton instance.Deduce
The Java equivalent would be ActivityResultContracts.PickVisualMedia.ImageOnly.INSTANCE. Writing it out this way in Java requires casting it to VisualMediaType. After doing so in Java, the compiler complains about it being an incompatible cast. It's especially weird because it doesn't complain about it when written that way in Kotlin. I even decompiled that same working Kotlin code into Java and then weirdly the compiler complains about it. Very weird.Acalia
A
13

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.

enter image description here

enter image description here

Acalia answered 8/10, 2022 at 19:48 Comment(5)
Is it possible to somehow hide this error in Android Studio?Methodize
PickVisualMediaRequest request = new PickVisualMediaRequest.Builder() .setMediaType(ActivityResultContracts.PickVisualMedia.ImageOnly.INSTANCE) .build(); Casting is not required but the IDE still shows error.Biennial
have you found any other solution? ?Mudlark
@GauravMandlik Update the dependency to androidx.activity:activity:1.7.0 and you should be good. They fixed the error with v1.7.0.Biennial
@GauravMandlik easiest solution to every new google libraries bugs is to wait up to 1-2 years until they fix itFlavorous
B
3

As of March 22, 2023. There is a fix just update androidx.activity:activity to version 1.7.0.

Biennial answered 27/3, 2023 at 23:53 Comment(0)
M
0

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

Monadism answered 24/8, 2023 at 12:2 Comment(0)
J
0

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

SingleMimeType as an alternative to VisualMediaType

Jameson answered 31/8, 2023 at 10:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.