diff --git a/app/src/main/java/com/deniscerri/ytdl/util/FileUtil.kt b/app/src/main/java/com/deniscerri/ytdl/util/FileUtil.kt index 20fa08f2..a8b9ffd6 100644 --- a/app/src/main/java/com/deniscerri/ytdl/util/FileUtil.kt +++ b/app/src/main/java/com/deniscerri/ytdl/util/FileUtil.kt @@ -10,6 +10,7 @@ import android.net.Uri import android.os.Build import android.os.Environment import android.provider.DocumentsContract +import android.provider.MediaStore import android.util.DisplayMetrics import android.util.Log import android.view.ViewGroup @@ -63,9 +64,41 @@ object FileUtil { if (!File(path).delete()){ DocumentFile.fromSingleUri(App.instance, Uri.parse(path))?.delete() } + deleteFileFromMediaStore(path) } } + private fun deleteFileFromMediaStore(path: String) { + val contentResolver = App.instance.contentResolver + val file = File(path) + val uri = MediaStore.Files.getContentUri("external") + + val selection: String + val selectionArgs: Array + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + val parentPath = file.parentFile?.absolutePath.orEmpty() + val primaryRoot = Environment.getExternalStorageDirectory().absolutePath + if (parentPath.startsWith(primaryRoot)) { + val trimmed = parentPath + .removePrefix(primaryRoot) + .removePrefix(File.separator) + val relativePath = if (trimmed.isEmpty()) "" else "$trimmed${File.separator}" + selection = MediaStore.MediaColumns.RELATIVE_PATH + " =? AND " + + MediaStore.MediaColumns.DISPLAY_NAME + " =?" + selectionArgs = arrayOf(relativePath, file.name) + } else { + // Non-primary storage: fall back to DATA query + selection = MediaStore.MediaColumns.DATA + " =?" + selectionArgs = arrayOf(file.absolutePath) + } + } else { + selection = MediaStore.MediaColumns.DATA + " =?" + selectionArgs = arrayOf(file.absolutePath) + } + contentResolver.delete(uri, selection, selectionArgs) + } + fun exists(path: String) : Boolean { val file = File(path) if (path.isEmpty()) return false