From 51351c2c82a637bc351a96072052a41e3aa6d60f Mon Sep 17 00:00:00 2001 From: yugal1107 Date: Fri, 26 Sep 2025 07:17:02 +0000 Subject: [PATCH 1/3] Fix: delete files from MediaStore when removed from app --- .../main/java/com/deniscerri/ytdl/util/FileUtil.kt | 11 +++++++++++ 1 file changed, 11 insertions(+) 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..87cecac4 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,19 @@ 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 = MediaStore.MediaColumns.DATA + " =?" + val selectionArgs = arrayOf(file.absolutePath) + contentResolver.delete(uri, selection, selectionArgs) + } + fun exists(path: String) : Boolean { val file = File(path) if (path.isEmpty()) return false From d8e1e46ae2794f6f27959e64de6f749280418e1d Mon Sep 17 00:00:00 2001 From: yugal1107 Date: Fri, 26 Sep 2025 09:05:07 +0000 Subject: [PATCH 2/3] Used scoped storage for media store deletion --- .../java/com/deniscerri/ytdl/util/FileUtil.kt | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) 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 87cecac4..ac5c7b41 100644 --- a/app/src/main/java/com/deniscerri/ytdl/util/FileUtil.kt +++ b/app/src/main/java/com/deniscerri/ytdl/util/FileUtil.kt @@ -72,8 +72,24 @@ object FileUtil { val contentResolver = App.instance.contentResolver val file = File(path) val uri = MediaStore.Files.getContentUri("external") - val selection = MediaStore.MediaColumns.DATA + " =?" - val selectionArgs = arrayOf(file.absolutePath) + + val selection: String + val selectionArgs: Array + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + val parentPath = file.parentFile?.absolutePath ?: "" + val externalStoragePath = Environment.getExternalStorageDirectory().absolutePath + val relativePath = if (parentPath.length > externalStoragePath.length && parentPath.startsWith(externalStoragePath)) { + parentPath.substring(externalStoragePath.length).removePrefix("/") + "/" + } else { + "" + } + selection = MediaStore.MediaColumns.RELATIVE_PATH + " =? AND " + MediaStore.MediaColumns.DISPLAY_NAME + " =?" + selectionArgs = arrayOf(relativePath, file.name) + } else { + selection = MediaStore.MediaColumns.DATA + " =?" + selectionArgs = arrayOf(file.absolutePath) + } contentResolver.delete(uri, selection, selectionArgs) } From acd5a4cd0053d387d6543735bc069d83a3d24a2e Mon Sep 17 00:00:00 2001 From: yugal1107 Date: Fri, 26 Sep 2025 09:18:26 +0000 Subject: [PATCH 3/3] Updated logic to consider external media also --- .../java/com/deniscerri/ytdl/util/FileUtil.kt | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) 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 ac5c7b41..a8b9ffd6 100644 --- a/app/src/main/java/com/deniscerri/ytdl/util/FileUtil.kt +++ b/app/src/main/java/com/deniscerri/ytdl/util/FileUtil.kt @@ -77,15 +77,21 @@ object FileUtil { val selectionArgs: Array if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { - val parentPath = file.parentFile?.absolutePath ?: "" - val externalStoragePath = Environment.getExternalStorageDirectory().absolutePath - val relativePath = if (parentPath.length > externalStoragePath.length && parentPath.startsWith(externalStoragePath)) { - parentPath.substring(externalStoragePath.length).removePrefix("/") + "/" + 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) } - selection = MediaStore.MediaColumns.RELATIVE_PATH + " =? AND " + MediaStore.MediaColumns.DISPLAY_NAME + " =?" - selectionArgs = arrayOf(relativePath, file.name) } else { selection = MediaStore.MediaColumns.DATA + " =?" selectionArgs = arrayOf(file.absolutePath)