I am not able to create any file using "System.IO.File.Create" API at following location with my app targeted to Android 11 ( Target SDK API 30).

  • /storage/emulated/0/Documents
  • The API which I use in my application to get above path is "Android.OS.Environment.GetExternalStoragePublicDirectory (Android.OS.Environment.DirectoryDocuments).Path".

    I have got to know that with Android 11 their is Scoped Storage compulsory to be used. But then I want to know how can we create files out of app specific folder (Internal shared storage\Android\data).

    The Exception I am getting while creating file is below:

  • Could not create file "/storage/emulated/0/Documents/abc.txt". File already exists.
  • But I think the above exception thrown is not real one.

    The Xamarin Forms version used : 4.8.0.1560.
    Visual Studio Version: VS2019 16.9.0

    Any help or guidance on this is appreciated. Thanks in Advance.

    Thanks.

    Hello,

    Welcome to our Microsoft Q&A platform!

    When we check document getExternalStoragePublicDirectory ,we will find that

    So, you can access content stored on shared/external storage by migrating to alternatives such as Context#getExternalFilesDir(String) , MediaStore , or Intent#ACTION_OPEN_DOCUMENT .

    And there is a article about this, you can check it here: Android 11 Scoped Storage - Saving Files To Shared Storage .

    Best Regards,

    Jessie Zhang

    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.

    @JessieZhang-MSFT Thanks for your inputs.

    So if I understand correctly we can now use only app specific external storage to Write and Read files.

    But as part of migration how can I copy my old files at below location to app specific external storage with my app targeting Android 11 to avoid loss of data for end users?

  • /storage/emulated/0/Documents
  • Any help or guidance on this is appreciated.

    Thanks.

    Hi @Muffadal Khopoliwala , I did a test based on the offical sample LocalFiles on android emulator (android 11), and changed the code in class ExternalStorageFilenameGenerator to yours, but it works properly.

    If it is convenient for you, could you please post a basic demo so that we can help you better?

    I was facing the same issue when using GetExternalStoragePublicDirectory and creating files under /Storage/emulated/0/Documents.
    Folder creation is successful, but when creating a new file, getting IOException saying that could not create file and File already exists.

    I see the issue is there even when using the https://github.com/xamarin/monodroid-samples/tree/master/LocalFiles repo also.
    It worked only for the very first time.
    Once the txt file is created, delete the file by going to Files app on Android phone, and try the app again. You will get the same exception.

    If the user deletes the file/folder manually, we are getting to see this issue.

    Any idea how do we resolve this?