How to save a file under Maui on Android in MyDocuments?

Tim W 21 Reputation points
2022-06-28T14:41:30.61+00:00

I would like to save a file on my Android device. It should be accessible to the user (e.g. by a file browser or simply when plugging the phone into a PC via USB).

FileSystem.AppDataDirectory ist not suitable for this, and Environment.GetFolderPath(Environment.SpecialFolder.MyMusic) keeps mapping to /data/0/user/com.package.name, which is not accessible by the File Browser app.

Basically: how do I save a file in Android outside the app sandbox?

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

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 41,661 Reputation points Microsoft Employee
    2022-06-29T05:50:05.71+00:00

    how do I save a file in Android outside the app sandbox?

    Firstly, you cannot save files in the external storage, it is related to Storage updates in Android 11, this update comes from native android's design.

    But you can Request All files access. Note: the Google Play store has a policy that limits usage of this permission.

    1.Declare the MANAGE_EXTERNAL_STORAGE permission in the manifest.
    2.Use the ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION intent action to direct users to a system settings page where they can enable the following option for your app: Allow access to manage all files.

    After that, you can save it in /storage/emulated/0/Documents path.

    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.


0 additional answers

Sort by: Most helpful