I'm developing an applicaiton using Android 11 and Kotlin. I've successfully written a file to the Download folder on the device. When writing to the folder again using the same file name, the result is another file with the same name. So now I have two files with the same name.
So first I thought I'd just delete the file then write it again. I spent hours and hours trying that to no avail. The delete code would execute without exception but the file would never delete. I'm pretty sure I set the proper permissions by using
if (!isWriteExternalStoragePermissionGranted()) {
val permissions = arrayOf(WRITE_EXTERNAL_STORAGE)
for (i in permissions.indices) {
requestPermission(permissions[i], i)
}
}
private fun isWriteExternalStoragePermissionGranted(): Boolean {
val permissionCheck = ActivityCompat.checkSelfPermission(this, WRITE_EXTERNAL_STORAGE)
return permissionCheck == PackageManager.PERMISSION_GRANTED
}
Then I thought I'd truncate the contents of the file and just overwrite the files contents. That didn't work. Just annother copy of file again and again. I have spent almpost a full day on this. It really shouldn't be this hard. I've tried numerous examples.. nothing works. Here's my code to write the file...
fun writeToFile(applicationContext: Context, filename: String, data: String) {
try {
val resolver = applicationContext.contentResolver
val values = ContentValues()
values.put(MediaStore.MediaColumns.DISPLAY_NAME, filename)
values.put(MediaStore.MediaColumns.MIME_TYPE, "text/xml")
values.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_DOWNLOADS)
val uri = resolver.insert(MediaStore.Files.getContentUri("external"), values)
//val cr: ContentResolver = getContentResolver()
val os: OutputStream? = uri?.let { resolver.openOutputStream(it,"wt") }
if (os != null) {
os.write(data.toByteArray())
os.flush()
os.close()
}
/*
if (uri != null) {
resolver.openFileDescriptor(uri, "wt")?.use {
FileOutputStream(it.fileDescriptor).use {
it.write(data.toByteArray()
)
}
}
}
*/
} catch (e: FileNotFoundException) {
e.printStackTrace()
} catch (e: IOException) {
e.printStackTrace()
}
}
Here's my code to delete the file first that never works. I've tried multiple variations...
fun deleteFile(context: Context, filename: String, applicationContext: Context){
val myFile = File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), filename)
if (myFile.exists()) {
myFile.delete()
}
}
fun deleteFile(context: Context, filename: String, applicationContext: Context){
val resolver = applicationContext.contentResolver
val values = ContentValues()
values.put(MediaStore.MediaColumns.DISPLAY_NAME, filename)
values.put(MediaStore.MediaColumns.MIME_TYPE, "text/xml")
values.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_DOWNLOADS)
val uri = resolver.insert(MediaStore.Files.getContentUri("external"), values)
if (uri != null) {
resolver.delete(uri, null, null)
}
}
CodePudding user response:
You should use the uri you got with the first insert() where you created the file.