From e849c6f7c01702f321843b68ec070a5cc8569b89 Mon Sep 17 00:00:00 2001 From: tibbi Date: Tue, 2 Nov 2021 23:30:43 +0100 Subject: [PATCH] use MediaStore for fetching files on Android 11 --- app/build.gradle | 2 +- .../gallery/pro/helpers/MediaFetcher.kt | 91 +++++++++++++++++++ 2 files changed, 92 insertions(+), 1 deletion(-) diff --git a/app/build.gradle b/app/build.gradle index 11b2286a3..ec5cc10bb 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -78,7 +78,7 @@ android { } dependencies { - implementation 'com.github.SimpleMobileTools:Simple-Commons:f1d626ef3d' + implementation 'com.github.SimpleMobileTools:Simple-Commons:b205a97a9f' implementation 'com.theartofdev.edmodo:android-image-cropper:2.8.0' implementation 'it.sephiroth.android.exif:library:1.0.1' implementation 'pl.droidsonroids.gif:android-gif-drawable:1.2.22' diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/MediaFetcher.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/MediaFetcher.kt index c89e552c2..7491d6e85 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/MediaFetcher.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/MediaFetcher.kt @@ -8,6 +8,7 @@ import android.net.Uri import android.os.Bundle import android.os.Environment import android.provider.BaseColumns +import android.provider.MediaStore import android.provider.MediaStore.Files import android.provider.MediaStore.Images import android.text.format.DateFormat @@ -393,6 +394,96 @@ class MediaFetcher(val context: Context) { return media } + @SuppressLint("InlinedApi") + fun getAndroid11FolderMedia( + isPickImage: Boolean, isPickVideo: Boolean, favoritePaths: ArrayList + ): HashMap> { + val media = HashMap>() + val filterMedia = context.config.filterMedia + val showHidden = context.config.shouldShowHidden + + val projection = arrayOf( + Images.Media.DISPLAY_NAME, + Images.Media.DATA, + Images.Media.DATE_MODIFIED, + Images.Media.DATE_TAKEN, + Images.Media.SIZE, + MediaStore.MediaColumns.DURATION + ) + + val uri = Files.getContentUri("external") + + context.queryCursor(uri, projection) { cursor -> + if (shouldStop) { + return@queryCursor + } + + try { + val filename = cursor.getStringValue(Images.Media.DISPLAY_NAME) + val path = cursor.getStringValue(Images.Media.DATA) + val lastModified = cursor.getLongValue(Images.Media.DATE_MODIFIED) * 1000 + val dateTaken = cursor.getLongValue(Images.Media.DATE_TAKEN) + val size = cursor.getLongValue(Images.Media.SIZE) + val videoDuration = Math.round(cursor.getIntValue(MediaStore.MediaColumns.DURATION) / 1000.toDouble()).toInt() + + val isPortrait = false + val isImage = path.isImageFast() + val isVideo = if (isImage) false else path.isVideoFast() + val isGif = if (isImage || isVideo) false else path.isGif() + val isRaw = if (isImage || isVideo || isGif) false else path.isRawFast() + val isSvg = if (isImage || isVideo || isGif || isRaw) false else path.isSvg() + + if (!isImage && !isVideo && !isGif && !isRaw && !isSvg) { + return@queryCursor + } + + if (isVideo && (isPickImage || filterMedia and TYPE_VIDEOS == 0)) + return@queryCursor + + if (isImage && (isPickVideo || filterMedia and TYPE_IMAGES == 0)) + return@queryCursor + + if (isGif && filterMedia and TYPE_GIFS == 0) + return@queryCursor + + if (isRaw && filterMedia and TYPE_RAWS == 0) + return@queryCursor + + if (isSvg && filterMedia and TYPE_SVGS == 0) + return@queryCursor + + if (!showHidden && filename.startsWith('.')) + return@queryCursor + + if (size <= 0L) { + return@queryCursor + } + + val type = when { + isVideo -> TYPE_VIDEOS + isGif -> TYPE_GIFS + isRaw -> TYPE_RAWS + isSvg -> TYPE_SVGS + isPortrait -> TYPE_PORTRAITS + else -> TYPE_IMAGES + } + + val isFavorite = favoritePaths.contains(path) + val medium = Medium(null, filename, path, path.getParentPath(), lastModified, dateTaken, size, type, videoDuration, isFavorite, 0L) + val parent = medium.parentPath.lowercase(Locale.getDefault()) + val currentFolderMedia = media[parent] + if (currentFolderMedia == null) { + media[parent] = ArrayList() + } + + media[parent]?.add(medium) + } catch (e: Exception) { + } + } + + return media + } + private fun getMediaOnOTG( folder: String, isPickImage: Boolean, isPickVideo: Boolean, filterMedia: Int, favoritePaths: ArrayList, getVideoDurations: Boolean