Cannot create file in "/storage/emulated/0/Documents" with Target SDK Android 11 (API 30)

Muffadal Khopoliwala 31 Reputation points
2021-03-04T16:08:45.977+00:00

Hi,

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.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,293 questions
{count} votes

3 answers

Sort by: Most helpful
  1. JessieZhang-MSFT 7,706 Reputation points Microsoft Vendor
    2021-03-05T08:39:52.263+00:00

    Hello,

    Welcome to our Microsoft Q&A platform!

    When we check document getExternalStoragePublicDirectory,we will find that

    74698-image.png

    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.

    2 people found this answer helpful.

  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  3. zxybsd 0 Reputation points
    2023-08-28T11:57:45.6733333+00:00

    Hola Espero haber entendido tu pregunta, dices que quieres saber como crear aplicaciones en una carpeta en especifico y segun tu experiencia yo lo realice con una aplicacion de uno de tus dispositivos o tienda de app a la cual se entro a ella de manera forzada es esto lo que estas preguntando cierto

    0 comments No comments