adding a helper extension for querying cursors
This commit is contained in:
parent
8985ca6222
commit
fe8e139fff
2 changed files with 26 additions and 1 deletions
|
@ -7,7 +7,7 @@ buildscript {
|
|||
propMinSdkVersion = 21
|
||||
propTargetSdkVersion = propCompileSdkVersion
|
||||
propVersionCode = 1
|
||||
propVersionName = '5.24.18'
|
||||
propVersionName = '5.24.19'
|
||||
kotlin_version = '1.3.71'
|
||||
}
|
||||
|
||||
|
|
|
@ -306,6 +306,31 @@ fun Context.getMediaContent(path: String, uri: Uri): Uri? {
|
|||
return null
|
||||
}
|
||||
|
||||
fun Context.queryCursor(
|
||||
uri: Uri,
|
||||
projection: Array<String>,
|
||||
selection: String? = null,
|
||||
selectionArgs: Array<String>? = null,
|
||||
sortOrder: String? = null,
|
||||
showErrors: Boolean = false,
|
||||
callback: (cursor: Cursor) -> Unit
|
||||
) {
|
||||
try {
|
||||
val cursor = contentResolver.query(uri, projection, selection, selectionArgs, sortOrder)
|
||||
cursor?.use {
|
||||
if (cursor.moveToFirst()) {
|
||||
do {
|
||||
callback(cursor)
|
||||
} while (cursor.moveToNext())
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
if (showErrors) {
|
||||
showErrorToast(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun Context.getFilenameFromUri(uri: Uri): String {
|
||||
return if (uri.scheme == "file") {
|
||||
File(uri.toString()).name
|
||||
|
|
Loading…
Reference in a new issue