From 06d728b285715e3dec7242f6099e03c657da32ea Mon Sep 17 00:00:00 2001 From: flan Date: Fri, 13 Apr 2018 15:02:59 +0200 Subject: [PATCH] Fix possible crash when Uri doesn't have a display name --- app/src/main/java/re/flande/xshare/Util.kt | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/re/flande/xshare/Util.kt b/app/src/main/java/re/flande/xshare/Util.kt index daf2e55..445473f 100644 --- a/app/src/main/java/re/flande/xshare/Util.kt +++ b/app/src/main/java/re/flande/xshare/Util.kt @@ -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) {