Skip to content

Commit

Permalink
Fix possible crash when Uri doesn't have a display name
Browse files Browse the repository at this point in the history
  • Loading branch information
tsudoko committed Apr 13, 2018
1 parent 6351352 commit 06d728b
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions app/src/main/java/re/flande/xshare/Util.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@ fun getFatalDialogBuilder(context: Activity): AlertDialog.Builder =
AlertDialog.Builder(context).setOnDismissListener { context.finishAffinity() }

fun Uri.getFilename(context: Context): String {
var name: String? = null
context.contentResolver.query(this, null, null, null, null).use { cursor ->
if (cursor?.moveToFirst() ?: return@use)
return cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME))
if (cursor?.moveToFirst() ?: return@use) {
name = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME))
}
}

if (scheme == "file")
return lastPathSegment

throw Exception("don't know how to get file name from $this")
return name ?: lastPathSegment ?: throw Exception("don't know how to get file name from $this")
}

fun View.fade(show: Boolean, duration: Long) {
Expand Down

0 comments on commit 06d728b

Please sign in to comment.