Created
September 5, 2017 02:01
-
-
Save maxost/447a6feec43c2ecdf1b22f8adb4e507d to your computer and use it in GitHub Desktop.
Kotlin: get file name in Android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private fun getFileName(context: Context, uri: Uri): String { | |
if (uri.scheme == "content") { | |
val cursor = context.contentResolver.query(uri, null, null, null, null) | |
cursor.use { | |
if(cursor.moveToFirst()) { | |
return cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)) | |
} | |
} | |
} | |
return uri.path.substring(uri.path.lastIndexOf('/') + 1) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment