dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
21.98k stars 1.72k forks source link

Use the new Photo Picker on Android 11+ in Essentials Picker API #9615

Open Redth opened 2 years ago

Redth commented 2 years ago

Description

With the latest AndroidX Activity release 1.6.0, there's a new API for picking media (photos/videos) which will automatically use the newer Android 11+ dialog or fall back to the older intent at runtime for older API levels.

Public API Changes

No public API changes should be necessary.

Intended Use-Case

Support the latest UX patterns on Android.

jamesmontemagno commented 2 years ago

🔥🔥🔥🔥 yes! <3 :)

jfversluis commented 2 years ago

Working on a spec for new MediaPicker stuff, I can include this. Although it might be maybe good to get this one in faster if we want to do it for .NET 7. The rest of what I'm planning will probably be .NET 8

janseris commented 2 years ago

The rest of what I'm planning will probably be .NET 8

So selecting multiple photos in MAUI takes 1 year and 3 months to implement?

jfversluis commented 2 years ago

The rest of what I'm planning will probably be .NET 8

So selecting multiple photos in MAUI takes 1 year and 3 months to implement?

No, but it will need breaking API changes which we are only allowed to introduce with major .NET versions

Alex-451 commented 1 year ago

@Redth the new Photo Picker seems to use the ActivityResultContracts.PickVisualMedia and MediaStore.ACTION_PICK_IMAGES.

I did not find the ACTION_PICK_IMAGES intent in the registered Android intents. Do I need to add that intent or can I solve this issue without having to eddit Android.Content.Intent?

As far as I know (I dont know what I am doing so I could be wrong 😅) I need that intent so I can update PickAsync in MediaPicker.android.cs:

        public async Task<FileResult> PickAsync(MediaPickerOptions options, bool photo)
        {
            var intent = new Intent(Intent.ActionGetContent);
            intent.SetType(photo ? FileMimeTypes.ImageAll : FileMimeTypes.VideoAll);

            var pickerIntent = Intent.CreateChooser(intent, options?.Title);

            try
            {
                string path = null;
                void OnResult(Intent intent)
                {
                    // The uri returned is only temporary and only lives as long as the Activity that requested it,
                    // so this means that it will always be cleaned up by the time we need it because we are using
                    // an intermediate activity.

                    path = FileSystemUtils.EnsurePhysicalPath(intent.Data);
                }

                await IntermediateActivity.StartAsync(pickerIntent, PlatformUtils.requestCodeMediaPicker, onResult: OnResult);

                return new FileResult(path);
            }
            catch (OperationCanceledException)
            {
                return null;
            }
        }

which then should solve the issue since var intent = new Intent(Intent.ActionPickImages); should change the way the dialog looks right?

salvadorjesus commented 2 months ago

Using the Photo Picker will be compulsory (*) from August 31th, 2024, for apps that do not declare the READ_MEDIA_IMAGES and/or the READ_MEDIA_VIDEO permissions.

Declaring those permission will be forbidden for "Apps with a one-time or limited use of photo and video files". More details about this on Google Play's Photo and Video Permissions policy. A common example is an app that allows you to select a profile picture from the gallery, but not much else.

(*) Well, actually, Google doesn't say it will be compulsory. It says that apps "are requested to use a system picker such as the Android photo picker". I reckon that Essentials uses a system picker, although not the new photo picker, and that that is enough.

To put that to the test, I deselected those permissions from my app Android manifest, made a clean build and made sure that the app was able to pick a photo from the gallery with no issues, with what appears to be a system picker (I did not delve into the Essentials code to make sure of that).

I thought that it would be a nice idea to mention this on this thread to enrich the conversation and to help people looking for information about the implications of this policy change.