android: replacing a crash with an alert when opening broken Uri (#4674)

* android: replacing a crash with an alert when opening broken Uri

* strings

---------

Co-authored-by: Evgeny <evgeny@poberezkin.com>
This commit is contained in:
Stanislav Dmitrenko
2024-08-13 17:42:33 +00:00
committed by GitHub
parent 38fa4c231f
commit c72c461306
4 changed files with 19 additions and 5 deletions
@@ -18,5 +18,6 @@ val NotificationsMode.requiresIgnoringBattery
lateinit var APPLICATION_ID: String
fun Uri.toURI(): URI = URI(toString().replace("\n", ""))
fun Uri.toURIOrNull(): URI? = try { toURI() } catch (e: Exception) { null }
fun URI.toUri(): Uri = Uri.parse(toString())
@@ -241,10 +241,15 @@ private fun calculateInSampleSize(options: BitmapFactory.Options, reqWidth: Int,
}
actual fun getFileName(uri: URI): String? {
return androidAppContext.contentResolver.query(uri.toUri(), null, null, null, null)?.use { cursor ->
val nameIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)
cursor.moveToFirst()
cursor.getString(nameIndex)
return try {
androidAppContext.contentResolver.query(uri.toUri(), null, null, null, null)?.use { cursor ->
val nameIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)
cursor.moveToFirst()
// Can make an exception
cursor.getString(nameIndex)
}
} catch (e: Exception) {
null
}
}