Home > OS >  Android 11 - Saving a .csv Log File to a Location that the User can Reliably Access Using Windows Fi
Android 11 - Saving a .csv Log File to a Location that the User can Reliably Access Using Windows Fi

Time:10-31

I am developing an application in Kotlin for Android 11. My app collects a log of sailing data from the boat instruments and internal GPS during a race.

I want to write a .csv file to some location where the User can access this using Windows File Explorer (connecting to the Android device with a USB cable) to copy and analyse the data in Windows.

I have been able to create and write .csv files to a subdirectory of Documents however if the User deletes the file using FileExplorer then Android 11 still reports that the file exists { filename.exists() returns true } when it doesn't, but worse won't delete what it thinks exists using { filename.delete() }, returns false for { filename.CanWrite() }, fails (returns false) to execute { filename.createNewFile()} and crashes if I try and write anything to the file.

How can I reliably provide the logged data to a User ??? Very happy to use any alternative approach that can get the log data to the User and not be damaged by File Explorer actions.

I successfully obtained User permission to write files to a subdirectory under Documents /storage/emulated/0/Documents/layline

The path to the Documents folder was obtained from: Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS)

The Manifest contains:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> android:requestLegacyExternalStorage="true"

and I successfully verified the permissions with: checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) retuns 0 checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) returns 0

I was able to read and write to a file OK until the file was deleted using Windows File Explorer. The files are Read Only from Windows perspective and if the file is edited and saved to a separate location and then copied back to overwrite the original, the file system starts presenting [1], [2] etc suffixes and Android 11 can't seem to get back at the file (it thinks that a version exists but can't delete or write to it).

Uninstalling and reinstalling the app does not fix the problem. Android 11 still reports that the file exists, but can't write to it.
filename.SetWritable() does not help (returns false).

Do I need to use All Files Access or Manage External Storage ?

CodePudding user response:

You can store your log files in /data/data/{your package name}/caches and then write to /storage/emulated/0/layline via Documents when users need to get log files

CodePudding user response:

As posted by @blackapps: If you delete a file from Documents directory and you cannot create a new one because Android tells you that the file still exists, then you have to delete the entry in the MediaStore first as apparently the file has been scanned/indexed by it.

  • Related