Reputation: 5
I am creating an app that can convert objects to JSON so that they can be imported/viewed by the app on another device. I am getting this error only when viewing files created by the app on another device. I have access to the folder this txt is placed in as I am able to create and write files of the same type (txt with the JSON) within the folder and read the ones created by the app on that device.
I have the following in the manifest:
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="32" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="28" />
<uses-permission android:name="android.permission.MANAGE_DOCUMENTS."/>
And android:requestLegacyExternalStorage="true"
in the <application tag.
And the provider:
<provider
android:authorities="com.example.listtrial.fileprovider"
android:name="androidx.core.content.FileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"
/>
</provider>
And the following is how I read the txt files:
val docFolder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS)
val exportsFold = File(docFolder, "ToeApp Exported Reports")
fun makeExportsFolder(){
if (!exportsFold.exists()){
exportsFold.mkdirs()
}
if (!toeAppDir.exists()){
val makecheck = toeAppDir.mkdirs()
Log.i("create File", "MakeCheck: $makecheck")
}
}
fun getExpRep(uri: Uri, context: Context): ExportedRep?{
val docFile = fromSingleUri(context, uri)
val name = docFile?.name
val path = uri.path?.split("/")
Log.i("Path last seg" , "${path?.last()}")
Log.i("file name" , "${name}")
Log.i("Path check", exportsFold.path)
if (name.isNotNull()){
try {
val filePath = File(exportsFold , name)
Log.i("path check" , "Filepath: $filePath")
val content = ByteArray(filePath.length().toInt())
val stream = FileInputStream(filePath)
stream.read(content)
val repJSON = content.decodeToString()
val expRep = Gson().fromJson(repJSON, ExportedRep::class.java)
return expRep
}
catch(e: Exception){
e.printStackTrace()
Toast.makeText(context, "Select Exported Report", Toast.LENGTH_SHORT).show()
return null
}
}
else return null
As mentioned the app is able to read/write JSON of the ExportedRep in the same folder so I don't believe its a permission issue. Its only throwing the exception when attempting to read the file created by the app on another device. I'm testing between two device that both run Android 14.
java.io.FileNotFoundException: /storage/emulated/0/Documents/ToeApp Exported Reports/Exported_Bruce Wayne_27_06_2024.txt: open failed: EACCES (Permission denied)
Thank you for the help!
Upvotes: 0
Views: 68
Reputation: 1007474
Its only throwing the exception when attempting to read the file created by the app on another device
You only have rights to files in Documents
that your app installation put there. If it was put there by the OS itself or another app -- including some previous installation of your app that was then uninstalled -- you have no rights to it.
Upvotes: 0