From d8e1e46ae2794f6f27959e64de6f749280418e1d Mon Sep 17 00:00:00 2001 From: yugal1107 Date: Fri, 26 Sep 2025 09:05:07 +0000 Subject: [PATCH] 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) }