question

MuffadalKhopoliwala-3129 avatar image
0 Votes"
MuffadalKhopoliwala-3129 asked VaranasiPrasanth-3918 commented

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

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.







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

JessieZhang-2116 avatar image
0 Votes"
JessieZhang-2116 answered VaranasiPrasanth-3918 commented

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.



image.png (46.4 KiB)
· 3
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.

@JessieZhang-2116 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.





0 Votes 0 ·
JessieZhang-2116 avatar image JessieZhang-2116 MuffadalKhopoliwala-3129 ·

Hi @MuffadalKhopoliwala-3129 , 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?


0 Votes 0 ·

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?

1 Vote 1 ·