17

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 need to disaplay video file name. How to get video content file name?

thank you

byungkyu
  • 211
  • 1
  • 5
  • 7
  • So you want to display the URI? Or split it by / and display the last part? – Mikhail Nov 24 '10 at 02:42
  • 1
    I want to display last part. if I display URI vALUE is /external/video/media/3. but 3 is id. not file name. – byungkyu Nov 24 '10 at 05:08
  • Look at this other post: [http://stackoverflow.com/questions/1105593/get-file-name-from-uri-string-in-c-sharp][1] [1]: http://stackoverflow.com/questions/1105593/get-file-name-from-uri-string-in-c-sharp – Jaime Botero Dec 13 '11 at 14:21
  • Google's answer: https://developer.android.com/training/secure-file-sharing/retrieve-info.html – MechEthan Apr 24 '15 at 23:32

10 Answers10

28

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();
Pratik
  • 30,639
  • 18
  • 84
  • 159
fargath
  • 7,844
  • 6
  • 24
  • 36
  • Not working in my case it only gives the name at the end of the url but while fetching the documents from the files inside of the Google drive or somewhere else it returns the string after the last forward slash. – cammando Mar 09 '17 at 11:39
10

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();
                }
            }
        }
alijandro
  • 11,627
  • 2
  • 58
  • 74
5

If the uri is a content provider uri, this is how you should retrieve file info.

        Cursor cursor = getContentResolver().query(uri, null, null, null, null);
        /*
         * Get the column indexes of the data in the Cursor,
         * move to the first row in the Cursor, get the data,
         * and display it.
         */
        int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
        cursor.moveToFirst();
        nameView.setText(cursor.getString(nameIndex));

https://developer.android.com/training/secure-file-sharing/retrieve-info.html

ben_joseph
  • 1,660
  • 1
  • 19
  • 24
3

For kotlin just simply:

val fileName = File(uri.path).name
Sam Chen
  • 7,597
  • 2
  • 40
  • 73
2

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.

Avinesh
  • 535
  • 4
  • 10
1
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)
        {

        }
Ayaz Alifov
  • 8,334
  • 4
  • 61
  • 56
  • Someone downvoted the answer. Could you, please, explain why? – Ayaz Alifov Apr 19 '17 at 11:44
  • @Grsmto, sorry, but did you even try to run the code before writing your comment? I think no, because I just checked your example url and it returned proper result - file.mp3 – Ayaz Alifov May 13 '17 at 02:58
  • I may be wrong, realised my Android Studio debugger is acting really weirdly...got some old cache of previous builds and all...I'll delete my answer. – adriendenat May 13 '17 at 20:06
1
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 :)

Piyush Patel
  • 1,765
  • 1
  • 16
  • 29
0

Google reference:

https://developer.android.com/training/secure-file-sharing/retrieve-info#RetrieveFileInfo

        /*
         * Get the file's content URI from the incoming Intent,
         * then query the server app to get the file's display name
         * and size.
         */
        Uri returnUri = returnIntent.getData();
        Cursor returnCursor =
                getContentResolver().query(returnUri, null, null, null, null);
        /*
         * Get the column indexes of the data in the Cursor,
         * move to the first row in the Cursor, get the data,
         * and display it.
         */
        int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
        int sizeIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE);
        returnCursor.moveToFirst();
        TextView nameView = (TextView) findViewById(R.id.filename_text);
        TextView sizeView = (TextView) findViewById(R.id.filesize_text);
        nameView.setText(returnCursor.getString(nameIndex));
        sizeView.setText(Long.toString(returnCursor.getLong(sizeIndex)));
Shahab Saalami
  • 862
  • 10
  • 18
0

Try the following code,

    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('/');
            if (cut != -1) {
                result = result.substring(cut + 1);
            }
        }
        return result;
    }
Codemaker2015
  • 12,190
  • 6
  • 97
  • 81
-2

Here i am giving you a sample code which will upload image from gallery

To extract filename/Imagename,Do the following

File f = new File(selectedPath);
System.out.println("Image name is   ::" + f.getName());//get name of file

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 1:
if (resultCode == YourActivity.RESULT_OK) {
Uri selectedImageUri = data.getData();
String selectedPath = getPath(selectedImageUri);
File f = new File(selectedPath);
System.out.println("Image name is   ::" + f.getName());//get name of file
}
}

Hope this will help

Ethaan
  • 11,291
  • 5
  • 35
  • 45
Lincy Laiju
  • 195
  • 4
  • 18