developer.android.com has nice example code for this:
https://developer.android.com/guide/topics/providers/document-provider.html
A condensed version to just extract the file name (assuming “this” is an Activity):
public String getFileName(Uri uri) {
String result = null;
if (uri.getScheme().equals("content")) {
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
try {
if (cursor != null && cursor.moveToFirst()) {
result = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
}
} finally {
cursor.close();
}
}
if (result == null) {
result = uri.getPath();
int cut = result.lastIndexOf("https://stackoverflow.com/");
if (cut != -1) {
result = result.substring(cut + 1);
}
}
return result;
}