How to delete external SD card file using SAF (storage access framework)

0

Basically I need to delete a file that I have in a recylcerView, the path I have is /storage/0617-4585/Imagenes/myimagen.jpg but at the time of doing file.delete() the file is still ...

I was reading that I must have writable permissions on the external SD card obtained through the SAF, I have not found or I do not know how the memory route obtained from the SAF is saved, as I check the write permissions on external SD Card and how can I delete the file.

This is the code I have so far

fun suprimir(file: File) {

    file.delete()
    if (file.exists()) {

        try {
            val parent = File(file.parent)
            val p_name = parent.name
            val f_name = file.name

            //     val uri = Uri.parse("$p_name%2F$f_name") %2F is /
            DocumentsContract.deleteDocument(contentResolver, "NO SE COMO COLOCAR URI")

        } catch (error: Exception) {
            Log.i("prueba", "error: $error ")
            startActivityForResult(Intent(Intent.ACTION_OPEN_DOCUMENT).addCategory(Intent.CATEGORY_OPENABLE).setType("image/*"), 42)
        }

    }
}

That's how I'm getting the data

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if (resultCode == Activity.RESULT_OK) {

        val uri = data?.data // Ruta seleccionada

        val takeFlags = (intent.flags and (Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION))
        contentResolver.takePersistableUriPermission(uri, takeFlags)
        grantUriPermission(packageName, uri, Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
    }
}
    
asked by Leonardo Henao 15.02.2018 в 18:44
source

0 answers