use the new way of querying contentresolver on api 30+
This commit is contained in:
parent
5202b77e8c
commit
851fddc67a
1 changed files with 28 additions and 7 deletions
|
@ -1,8 +1,11 @@
|
||||||
package com.simplemobiletools.gallery.pro.helpers
|
package com.simplemobiletools.gallery.pro.helpers
|
||||||
|
|
||||||
|
import android.annotation.SuppressLint
|
||||||
|
import android.content.ContentResolver
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.database.Cursor
|
import android.database.Cursor
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
|
import android.os.Bundle
|
||||||
import android.os.Environment
|
import android.os.Environment
|
||||||
import android.provider.BaseColumns
|
import android.provider.BaseColumns
|
||||||
import android.provider.MediaStore.Files
|
import android.provider.MediaStore.Files
|
||||||
|
@ -97,19 +100,37 @@ class MediaFetcher(val context: Context) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressLint("NewApi")
|
||||||
private fun getLatestFileFolders(): LinkedHashSet<String> {
|
private fun getLatestFileFolders(): LinkedHashSet<String> {
|
||||||
val uri = Files.getContentUri("external")
|
val uri = Files.getContentUri("external")
|
||||||
val projection = arrayOf(Images.ImageColumns.DATA)
|
val projection = arrayOf(Images.ImageColumns.DATA)
|
||||||
val parents = LinkedHashSet<String>()
|
val parents = LinkedHashSet<String>()
|
||||||
val sorting = "${BaseColumns._ID} DESC LIMIT 50"
|
|
||||||
var cursor: Cursor? = null
|
var cursor: Cursor? = null
|
||||||
try {
|
try {
|
||||||
cursor = context.contentResolver.query(uri, projection, null, null, sorting)
|
if (isRPlus()) {
|
||||||
if (cursor?.moveToFirst() == true) {
|
val bundle = Bundle().apply {
|
||||||
do {
|
putInt(ContentResolver.QUERY_ARG_LIMIT, 10)
|
||||||
val path = cursor.getStringValue(Images.ImageColumns.DATA) ?: continue
|
putStringArray(ContentResolver.QUERY_ARG_SORT_COLUMNS, arrayOf(BaseColumns._ID))
|
||||||
parents.add(path.getParentPath())
|
putInt(ContentResolver.QUERY_ARG_SORT_DIRECTION, ContentResolver.QUERY_SORT_DIRECTION_DESCENDING)
|
||||||
} while (cursor.moveToNext())
|
}
|
||||||
|
|
||||||
|
cursor = context.contentResolver.query(uri, projection, bundle, null)
|
||||||
|
if (cursor?.moveToFirst() == true) {
|
||||||
|
do {
|
||||||
|
val path = cursor.getStringValue(Images.ImageColumns.DATA) ?: continue
|
||||||
|
parents.add(path.getParentPath())
|
||||||
|
} while (cursor.moveToNext())
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
val sorting = "${BaseColumns._ID} DESC LIMIT 10"
|
||||||
|
cursor = context.contentResolver.query(uri, projection, null, null, sorting)
|
||||||
|
if (cursor?.moveToFirst() == true) {
|
||||||
|
do {
|
||||||
|
val path = cursor.getStringValue(Images.ImageColumns.DATA) ?: continue
|
||||||
|
mydebug("path $path")
|
||||||
|
parents.add(path.getParentPath())
|
||||||
|
} while (cursor.moveToNext())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
context.showErrorToast(e)
|
context.showErrorToast(e)
|
||||||
|
|
Loading…
Reference in a new issue