8

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?

David Read
  • 1,089
  • 1
  • 9
  • 23
  • 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. – CommonsWare Oct 08 '22 at 19:05
  • 2
    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. – David Read Oct 08 '22 at 19:40

4 Answers4

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

David Read
  • 1,089
  • 1
  • 9
  • 23
  • 2
    Is it possible to somehow hide this error in Android Studio? – Mikhail Nov 20 '22 at 19:33
  • 4
    ```PickVisualMediaRequest request = new PickVisualMediaRequest.Builder() .setMediaType(ActivityResultContracts.PickVisualMedia.ImageOnly.INSTANCE) .build(); ``` Casting is not required but the IDE still shows error. – Roney Thomas Jan 06 '23 at 21:11
  • have you found any other solution? ? – Gaurav Mandlik Feb 14 '23 at 08:32
  • @GauravMandlik Update the dependency to androidx.activity:activity:1.7.0 and you should be good. They fixed the error with v1.7.0. – Roney Thomas Mar 27 '23 at 23:50
  • @GauravMandlik easiest solution to every new google libraries bugs is to wait up to 1-2 years until they fix it – cmak Apr 02 '23 at 15:32
2

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

Roney Thomas
  • 1,371
  • 1
  • 9
  • 11
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

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

Nebobrod
  • 11
  • 3