question

Phunction-1104 avatar image
0 Votes"
Phunction-1104 asked RobCaplan edited

Xamarin Android storage wars

Hi, I am having a fun time with the new Android 11 file permissions.
I copied a file over from my PC (via usb) to the devices download folder.
However, my app will not show the file in the list when I do System.IO.Directory.GetFiles(Path)
All the other files that are in there appear, but not any files I copy from my PC, how do I make that work? It works fine on earlier Android versions.

Path is from Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads)

dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

LeonLu-MSFT avatar image
0 Votes"
LeonLu-MSFT answered LeonLu-MSFT commented

Hello,​

Welcome to our Microsoft Q&A platform!

If you user could try to grand the android.permission.MANAGE_EXTERNAL_STORAGE,

First of all, add this permission in your AndroidManifest.xml.

<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />


Then, use Snackbar to grand the permission.

if (!Environment.IsExternalStorageManager)
            {
                Snackbar.Make(FindViewById(Android.Resource.Id.Content), "Permission needed!", Snackbar.LengthIndefinite)
                        .SetAction("Settings", new MyOnClickListener(this)).Show();
            }
  internal class MyOnClickListener:Java.Lang.Object,View.IOnClickListener
    {
        private MainActivity mainActivity;
        public MyOnClickListener()
        {
        }
        public MyOnClickListener(MainActivity mainActivity)
        {
            this.mainActivity = mainActivity;
        }
        public void OnClick(View v)
        {
            // throw new System.NotImplementedException();ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION
            try
            {
                mainActivity.StartActivity(new Intent(
    Android.Provider.Settings.ActionManageAllFilesAccessPermission,
    Android.Net.Uri.Parse("package:" + Android.App.Application.Context.PackageName)));
               
            }
            catch (Exception ex)
            {
                Intent intent = new Intent();
                intent.SetAction(Android.Provider.Settings.ActionManageAllFilesAccessPermission);
                mainActivity.StartActivity(intent);
            }
        }
    }
}


After user grand this permission, you can check it.

Best Regards,

Leon Lu



If the response is helpful, please click "Accept Answer" and upvote it.

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.


· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

My app does get permission, I can create, read file to that folder in the app fine, the problem is when a file is copied from a pc to the same folder, it is not accessible to the app.

0 Votes 0 ·

uninstall your application, after that, please reinstall it and grand the MANAGE_EXTERNAL_STORAGE permission, if it works?

0 Votes 0 ·