Skip to content

Instantly share code, notes, and snippets.

@maxost
Created September 5, 2017 02:01
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxost/447a6feec43c2ecdf1b22f8adb4e507d to your computer and use it in GitHub Desktop.
Save maxost/447a6feec43c2ecdf1b22f8adb4e507d to your computer and use it in GitHub Desktop.
Kotlin: get file name in Android
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