Hello I am develop video player with android gallery. I receive URI from gallry. and I need to display video title, when play video. so if content has not title meta data. I
int actual_image_column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
String filename = cursor.getString(actual_image_column_index);
This is example for Image . you can try same thing for your needs ( video).
Thanks & Best Luck :)
try
{
String uriString = "http://somesite.com/video.mp4";
URI uri = new URI(uriString);
URL videoUrl = uri.toURL();
File tempFile = new File(videoUrl.getFile());
String fileName = tempFile.getName();
}
catch (Exception e)
{
}
Here is the code for getting name of file from url
Uri u = Uri.parse("www.google.com/images/image.jpg");
File f = new File("" + u);
f.getName();
Since none of the answers really solve the questions, i put my solution here, hope it will be helpful.
String fileName;
if (uri.getScheme().equals("file")) {
fileName = uri.getLastPathSegment();
} else {
Cursor cursor = null;
try {
cursor = getContentResolver().query(uri, new String[]{
MediaStore.Images.ImageColumns.DISPLAY_NAME
}, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
fileName = cursor.getString(cursor.getColumnIndex(MediaStore.Images.ImageColumns.DISPLAY_NAME));
Log.d(TAG, "name is " + fileName);
}
} finally {
if (cursor != null) {
cursor.close();
}
}
}
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.
For kotlin just simply:
val fileName = File(uri.path).name