Below code works for me in case of image from gallery, It should work for any file type.
String fileName = "default_file_name";
Cursor returnCursor =
getContentResolver().query(YourFileUri, null, null, null, null);
try {
int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
returnCursor.moveToFirst();
fileName = returnCursor.getString(nameIndex);
LOG.debug(TAG, "file name : " + fileName);
}catch (Exception e){
LOG.error(TAG, "error: ", e);
//handle the failure cases here
} finally {
returnCursor.close();
}
Here you can find detailed explanation and much more.