From a4c590ddd3edfc3080a72505cd22ef3fecb73911 Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 28 Oct 2020 20:06:34 +0100 Subject: [PATCH] insert created nomedia files into MediaStore manually --- .../gallery/pro/extensions/Activity.kt | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Activity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Activity.kt index 2c9b54f0c..1fcd09510 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Activity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Activity.kt @@ -3,6 +3,7 @@ package com.simplemobiletools.gallery.pro.extensions import android.annotation.TargetApi import android.app.Activity import android.content.ContentProviderOperation +import android.content.ContentValues import android.content.Intent import android.graphics.Bitmap import android.graphics.BitmapFactory @@ -13,6 +14,7 @@ import android.media.ExifInterface import android.net.Uri import android.os.Build import android.provider.MediaStore +import android.provider.MediaStore.Files import android.provider.MediaStore.Images import android.util.DisplayMetrics import android.view.View @@ -147,9 +149,8 @@ fun BaseSimpleActivity.addNoMedia(path: String, callback: () -> Unit) { val fileDocument = getDocumentFile(path) if (fileDocument?.exists() == true && fileDocument.isDirectory) { fileDocument.createFile("", NOMEDIA) - applicationContext.scanPathRecursively(file.absolutePath) { - callback() - } + addNoMediaIntoMediaStore(file.absolutePath) + callback() } else { toast(R.string.unknown_error_occurred) callback() @@ -158,7 +159,7 @@ fun BaseSimpleActivity.addNoMedia(path: String, callback: () -> Unit) { } else { try { if (file.createNewFile()) { - rescanFolderMedia(file.absolutePath) + addNoMediaIntoMediaStore(file.absolutePath) } else { toast(R.string.unknown_error_occurred) } @@ -169,6 +170,19 @@ fun BaseSimpleActivity.addNoMedia(path: String, callback: () -> Unit) { } } +fun BaseSimpleActivity.addNoMediaIntoMediaStore(path: String) { + try { + val content = ContentValues().apply { + put(Files.FileColumns.TITLE, NOMEDIA) + put(Files.FileColumns.DATA, path) + put(Files.FileColumns.MEDIA_TYPE, Files.FileColumns.MEDIA_TYPE_NONE) + } + contentResolver.insert(Files.getContentUri("external"), content) + } catch (e: Exception) { + showErrorToast(e) + } +} + fun BaseSimpleActivity.removeNoMedia(path: String, callback: (() -> Unit)? = null) { val file = File(path, NOMEDIA) if (!getDoesFilePathExist(file.absolutePath)) {