use MediaStore for fetching files on Android 11

This commit is contained in:
tibbi 2021-11-02 23:30:43 +01:00
parent d97d338c4c
commit e849c6f7c0
2 changed files with 92 additions and 1 deletions

View file

@ -78,7 +78,7 @@ android {
} }
dependencies { 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 'com.theartofdev.edmodo:android-image-cropper:2.8.0'
implementation 'it.sephiroth.android.exif:library:1.0.1' implementation 'it.sephiroth.android.exif:library:1.0.1'
implementation 'pl.droidsonroids.gif:android-gif-drawable:1.2.22' implementation 'pl.droidsonroids.gif:android-gif-drawable:1.2.22'

View file

@ -8,6 +8,7 @@ import android.net.Uri
import android.os.Bundle import android.os.Bundle
import android.os.Environment import android.os.Environment
import android.provider.BaseColumns import android.provider.BaseColumns
import android.provider.MediaStore
import android.provider.MediaStore.Files import android.provider.MediaStore.Files
import android.provider.MediaStore.Images import android.provider.MediaStore.Images
import android.text.format.DateFormat import android.text.format.DateFormat
@ -393,6 +394,96 @@ class MediaFetcher(val context: Context) {
return media return media
} }
@SuppressLint("InlinedApi")
fun getAndroid11FolderMedia(
isPickImage: Boolean, isPickVideo: Boolean, favoritePaths: ArrayList<String>
): HashMap<String, ArrayList<Medium>> {
val media = HashMap<String, ArrayList<Medium>>()
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<Medium>()
}
media[parent]?.add(medium)
} catch (e: Exception) {
}
}
return media
}
private fun getMediaOnOTG( private fun getMediaOnOTG(
folder: String, isPickImage: Boolean, isPickVideo: Boolean, filterMedia: Int, favoritePaths: ArrayList<String>, folder: String, isPickImage: Boolean, isPickVideo: Boolean, filterMedia: Int, favoritePaths: ArrayList<String>,
getVideoDurations: Boolean getVideoDurations: Boolean