Google Play Permissions: Transitioning to Android PhotoPicker #25040
Replies: 2 comments 2 replies
-
For any wandering eyes, we did migrate to use the Android PhotoPicker. Instead of calling public static partial class MediaHelper
{
private static ActivityResultLauncher _pickMediaLauncher;
private static TaskCompletionSource<Android.Net.Uri> _photoPickerTcs;
/// <summary>
/// Initializes the Photo Picker launcher for Android. This should be called before the activity is resumed.
/// </summary>
/// <param name="activity">The Android activity that will register the launcher.</param>
public static void InitializePhotoPickerLauncher( Activity activity )
{
if ( _pickMediaLauncher == null )
{
// Register the activity result launcher before the activity is started or resumed
_pickMediaLauncher = (activity as AndroidX.Activity.ComponentActivity).RegisterForActivityResult(
new ActivityResultContracts.PickVisualMedia(),
new ActivityResultCallback( uri =>
{
// Set the result to TaskCompletionSource
_photoPickerTcs?.SetResult( uri );
} )
);
}
}
/// <summary>
/// An asynchronous method to pick a photo on Android, using AndroidX PhotoPicker.
/// </summary>
public static async Task<FileResult> PickAndroidPhotoAsync( MediaPickerOptions options = null )
{
// Create a new TaskCompletionSource to await the result
_photoPickerTcs = new TaskCompletionSource<Android.Net.Uri>();
// Launch the PhotoPicker for images
var pickVisualMediaRequest = new PickVisualMediaRequest.Builder()
.SetMediaType( ActivityResultContracts.PickVisualMedia.ImageOnly.Instance )
.Build();
_pickMediaLauncher.Launch( pickVisualMediaRequest );
// Return the Task, which will be completed when the user selects a photo or cancels
var uri = await _photoPickerTcs.Task;
if ( uri != null )
{
// Get the file data from the URI using the Android content resolver
using ( var stream = Platform.CurrentActivity.ContentResolver.OpenInputStream( uri ) )
{
// You can read the stream or return it as part of the FileResult
var tempFilePath = Path.Combine( FileSystem.CacheDirectory, $"{Guid.NewGuid()}.png" );
// Save the stream to a temporary file
using ( var fileStream = new FileStream( tempFilePath, FileMode.Create, FileAccess.Write ) )
{
await stream.CopyToAsync( fileStream );
}
// Return the FileResult for the temporary file
return new FileResult( tempFilePath );
}
}
else
{
return null;
}
}
/// <summary>
/// Handles the result of the photo picker selection.
/// </summary>
private class ActivityResultCallback : Java.Lang.Object, IActivityResultCallback
{
private readonly Action<Android.Net.Uri> _onResult;
public ActivityResultCallback( Action<Android.Net.Uri> onResult )
{
_onResult = onResult;
}
public void OnActivityResult( Java.Lang.Object result )
{
if ( result is Android.Net.Uri uri )
{
// Call the action with the selected URI
_onResult( uri );
}
else
{
// No media selected, pass null to the action
_onResult( null );
}
}
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
We just ran into this when trying to promote a release to production on the Android Play Store. All we did was remove the READ_MEDIA_IMAGES, READ_MEDIA_VIDEO, and READ_MEDIA_AUDIO permissions and now the Maui MediaPicker prompts the user for permission and the Google Play Store no longer gives the warning. |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hey there!
First and foremost, a huge thanks to everyone on the MAUI team (including contributors). I've been around since the Xamarin.Forms days and have transitioned through .NET 6 — the improvements since then have been remarkable!
I have a question regarding accessing the photo library on Android. Currently, we’re using the standard API as recommended in the Microsoft documentation:
This approach requires adding the following permissions to the Android manifest:
Recently, we encountered the following error in the Google Play Console:
While we can submit a declaration explaining the need for these permissions, the terminology suggests that the recommended approach moving forward is to adopt the Android
PhotoPicker
. You can read more about this shift in policy here.Would this change be something that should be reflected in .NET MAUI? Should I open an issue to address this, or is it already being considered?
Thanks,
Braden
Beta Was this translation helpful? Give feedback.
All reactions