0

I am targeting Android API 30. My app was storing log file and taking database backup in location "/storage/emulated/0/SpecialDir/". Now I am facing access denied issue while my app was workinng fine previously. I got an overview about scoped storage and came to know that we have some managed locaitons where we can store our data accordingly. i.e Audio, Video, Images, and Download

  • My question is What is the solution for existing apps that was previously saving files on "/storage/emulated/0/SpecialDir/".

Can anyone please guide me what should i do.

string dir = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.ToString(), "LogFolder"); if (Directory.Exists(dir)) { return Path.Combine(dir, "MyLogFile.txt"); } try { string newDirectory = Directory.CreateDirectory(dir).FullName; path = Path.Combine(newDirectory, "MyLogFile.txt"); System.IO.File.WriteAllText(path, "This is some testing log."); } catch (Exception ex) { string msg = ex.Message; }

The above code is used to make 'LogFolder' if not exist and 'MyLogFile' as well. What changes do i needed to make it compatiable to Android 10. Thankyou

1 Answer 1

2

In Android 10, Google has introduced a new feature for external Storage. Its name is Scoped Storage. Google officially translates it as partitioned Storage, or Scoped Storage.The intent is to limit what programs can do with public directories in external storage. Partitioned storage has no effect on either the internal storage private directory or the external storage private directory.In short, in Android 10, there is no change to private directory reads and writes, and you can still use the File set without any permissions. For reading and writing to public directories, you must use the API provided by MediaStore or the SAF (storage access framework), which means you can no longer use the File set to manipulate public directories at will.

If you set targetSdkVersion above 29,you could try to add below codes into your AndroidManifest.Then you could access the File as before.

<manifest ... > <application android:requestLegacyExternalStorage="true" ... > ... </application> </manifest> 

Update (you could try this for public external storage ):

var path = Android.OS.Environment.GetExternalStoragePublicDirectory("LogFolder").AbsolutePath; Java.IO.File file = new Java.IO.File(path); if (!file.Exists()) { file.Mkdirs(); } try { FileWriter fw = new FileWriter(path + Java.IO.File.Separator + "MyLogFile.txt"); fw.Write("This is some testing log."); fw.Close(); } catch (Exception ex) { string msg = ex.Message; } 

Update for Android 11:

add MANAGE_EXTERNAL_STORAGE permission in your AndroidManifest.

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

in your activity:

 if (Environment.IsExternalStorageManager) { var path = Android.OS.Environment.GetExternalStoragePublicDirectory("LogFolder").AbsolutePath; Java.IO.File file = new Java.IO.File(path); if (!file.Exists()) { file.Mkdirs(); } try { FileWriter fw = new FileWriter(path + Java.IO.File.Separator + "MyLogFile.txt"); fw.Write("This is some testing log."); fw.Close(); } catch (Exception ex) { string msg = ex.Message; } } else { StartActivityForResult(new Intent(Settings.ActionManageAllFilesAccessPermission), 0); } 
Sign up to request clarification or add additional context in comments.

7 Comments

thankyou sir for your reply. It is difficult to understand what you described above. I have added 'requestLegacyExternalStorage' for android 10 support but I need help for android 11. Can you please have a look on above code and suggest a suitable solution with example. Many thanks
I tried with 'string dirPath = Android.App.Application.Context.GetExternalFilesDir("").AbsolutePath;' my file and folder get created in package directory but it get deleted when I uninstall the app. . . .
@user6159419 Yes,string dirPath = Android.App.Application.Context.GetExternalFilesDir("").AbsolutePath; it's Internal storage,files will be deleted when the app is uninstalled.You could check above i update when you use external storage.
Zhu many thanks for your reply and taking interest to solve the issue. You can notice that I have mentioned Android API 30 which is Android-11. Do you think that your code will work for Android-11. Can you please confirm this, because I have doubt about it.
and also I want the persistant storage/folder/location where files don't get deleted after app uninstallation.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.