.Net Maui, ability to access external sd card

Phunction 206 Reputation points
2024-05-15T16:07:34.8933333+00:00

With the removal of read and write permissions to external storage, is there any way to access external SD cards?

My app normally relies on being able to automatically backup data to external SD cards in the event the tablet dies or is broken.

Is there any built in functionality to allow this in .Net Maui so it will still be accpeted by the Play Store?

These are custom binary files.

This is mainly for Android devices, but I would like to add to iOS as well as they can take thumb drives.

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,993 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 69,616 Reputation points Microsoft Vendor
    2024-05-24T03:07:18.1633333+00:00

    Hello,

    I assume I have to find a way to write some platform specific code to ask for this permission?

    Yes, this permission needs to open the android application settings page, then grand this permission by users.

    You can use Conditional compilation for android platform code.

    For example, you want to request this permission in the button click event. you can popup a SnackBar, tell user about this permission needed and let user to click.

    private void OnCounterClicked(object sender, EventArgs e)
            {
    #if ANDROID
                var activity=Platform.CurrentActivity;
                if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.R)
                {
                    if (!Android.OS.Environment.IsExternalStorageManager)
                    {
                        Google.Android.Material.Snackbar.Snackbar.Make(activity.FindViewById(Android.Resource.Id.Content), "Permission needed!", Google.Android.Material.Snackbar.Snackbar.LengthIndefinite)
                                .SetAction("Settings", new MyClickHandler()).Show();
                    }
     
                }
    #endif
            }
    

    Next, you need to implement the Android.Views.View.IOnClickListener interface for SnackBar. It will open the application settings page; user need to grand the manager external permission.

    #if ANDROID
        internal class MyClickHandler : Java.Lang.Object, Android.Views.View.IOnClickListener
        {
    
     
            public void OnClick(Android.Views.View v)
            {
                try
                {
     
                    Android.Net.Uri uri = Android.Net.Uri.Parse("package:" + Platform.CurrentActivity.ApplicationInfo.PackageName);
                   Android.Content.Intent intent = new Android.Content.Intent(Android.Provider.Settings.ActionManageAppAllFilesAccessPermission, uri);
                    Platform.CurrentActivity.StartActivity(intent);
                }
                catch (Exception ex)
                {
                    Android.Content.Intent intent = new Android.Content.Intent();
                    intent.SetAction(Android.Provider.Settings.ActionManageAppAllFilesAccessPermission);
                    Platform.CurrentActivity.StartActivity(intent);
                }
            }
        }
     
    #endif
    

    In the end, please open the AndroidManifest.xml and add <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" /> permission.

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.