0

I am unable delete txt file from download directory. File.delete() does not work. I have also tried with context.deleteFile(), that does not work either. I am not getting any exception, just nothing happens. This works on many Android devices, but it does not work on the device that I needed it to work, which is Android device with embedded printer. Android version 8.1.

I have found this answer, stackoverflow, which is similar to official documentation Google.

I have tried to use Uri.fromFile(directory) and directory.toUri() instead of MediaStore.Images.Media.EXTERNAL_CONTENT_URI from the example, since file is in download folder, but I got null pointer exception.

How I can get correct download folder Uri? Or is there another way to delete file?

Thank you in advance for any help and suggestions.

EDIT:

getAbsolutePath() = /storage/emulated/0/Download/print.txt

Code:

val downloadPath: File = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
// dowloadPath is passed as directory
fun readFile(directory: File, context: Context): String {
    val fileToRead = File(directory, "print.txt")
    //...
    if (fileToRead.exists()) {
        //...
        fileToRead.delete()
    }
}

Permissions:

<uses-permission
    android:name="android.permission.READ_EXTERNAL_STORAGE"
    android:maxSdkVersion="32" />
<uses-permission
    android:name="android.permission.WRITE_EXTERNAL_STORAGE"
    android:maxSdkVersion="29" />

And inside <application

android:requestLegacyExternalStorage="true"
android:requestRawExternalStorageAccess="true"
Mike Irving
  • 1,480
  • 1
  • 12
  • 20
look_up
  • 41
  • 8
  • You have to use content resolver – Style-7 Nov 24 '22 at 12:51
  • 1
    For an Android 8 device having the normal write permission in manifest and at runtime File.delete() should work. No need to mess around with uries and content resolvers. – blackapps Nov 24 '22 at 13:13
  • 1
    Please tell value of file.getAbsolutePath(). And show your code. – blackapps Nov 24 '22 at 13:15
  • Please do not post code to read that file. Only to delete it. One delete() only! Do not only tell the path but also post such code that we can check if you determined the path correct. – blackapps Nov 24 '22 at 15:39
  • @blackapps, I have edited code section, and in readFile() is where file needs to be deleted. – look_up Nov 24 '22 at 16:07
  • Interesting, `fileToRead.canRead()` returns `true`, but `fileToRead.canWrite()` returns `false`... – look_up Nov 24 '22 at 16:23
  • 1
    Did you request write permission at runtime? – blackapps Nov 24 '22 at 16:37
  • `android:requestRawExternalStorageAccess="true"` I never saw such. For what is it used? – blackapps Nov 24 '22 at 16:39
  • @blackapps, that is the problem. I requested read, but not write. So stupid. For a RowAccess, I have found it in some answer and I just added it, and it stayed. It does not have impact on API<31. [requestRawExternalStorageAccess](https://developer.android.com/reference/android/R.attr.html#requestRawExternalStorageAccess). Thank you for help. – look_up Nov 24 '22 at 17:18

0 Answers0