Having just tested using your above setup in a AndrioidManifest.xml
:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
android:requestLegacyExternalStorage="true"
With those permissions granted and on a emulator running API 30 (R) I was able to write/read/update a text file with this simple code :
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
if(ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
val f = File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "new_file.txt")
f.appendText("test ${Instant.now().toEpochMilli()}\n")
f.readLines().forEach { line -> Log.e("LOG", line)}
}
}
This post : https://mcmap.net/q/89290/-write-permissions-not-working-scoped-storage-android-sdk-30-aka-android-11 suggests that if you created the file you will be able to have access to it using the File api. Note : Environment.getExternalStoragePublicDirectory
is deprecated but seems to work even on Android 11 if you are the owner of the file.
Just for fun I swapped between target and compile versions 29/30 to see if anything would blow up when targetting different sdk's and reinstalling the same app on the same emulator. It worked fine, I had full access to the same file regardless.
If I'm honest the whole thing is a bit of a shambles - this post by CommonsWare https://commonsware.com/blog/2019/12/21/scoped-storage-stories-storing-mediastore.html is a good read as it touches on many things that are now enforced in Android 11, even though it mostly talks about Android 10.
Documentation seems to be splintered with sections pertaining to storage / scoped storage and the like in a lot of different places. This link to a table gives a good launch ground for sifting through the documentation based on inital use case : https://developer.android.com/training/data-storage
I have also attached a screenshot of the file appearing in the file manager :
PS : Terrible throwaway code here - I/O work on main thread etc .. only for illustrative purposes.
Based on comment "R != 30". My usages of "R" and Api "30" are based on this from the AndroidStudio IDE :
(R = runtime environment, Api 30 = sdk for runtime)
Happy for an edit if I have misunderstood something, or not semantically correct in some way.