I am working on an app that has a “report” object. The idea is to be able to export the report to a .txt file as a JSON to external storage (public), and it should also be able to read .txt files (also from external storage) to get the JSON and recreate the instance of the object from the txt. The idea is there could be multiple files so the user can pick the specific file they want to use. Currently, the app is able to save the .txt file with no issue, at the correct location, and when I opened the file externally it is correct. However when trying to open the file i get the error:
java.io.FileNotFoundException: /document/primary:Documents/.../27_06_2024.txt: open failed: ENOENT (No such file or directory)
To try to open the file I used the approach from: https://fvilarino.medium.com/using-activity-result-contracts-in-jetpack-compose-14b179fb87de
With this I am able to use system file picker to get the correct uri of the file.
I attempt to get the data from the .txt file using the uri of the file as follows:
fun getExpRep(uri: Uri): ExportedRep{
val path = uri.path
val content = path?.let { ByteArray(it.length) }
val stream = FileInputStream(path)
stream.read(content)
val repJSON = content?.decodeToString()
val expRep = Gson().fromJson(repJSON, ExportedRep::class.java)
return expRep
}
The error is in the “val content = path?.let { ByteArray(it.length) }” line. The filepath specified in the exception is also actually correct, so I have no idea why its saying the file does not exist (as mentioned, ive opened it). And since the file is also created and placed in that folder by the app, I dont think it is a permissions issue.