how to get file name from URI

孤人 提交于 2019-11-27 17:57:29

问题


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


回答1:


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();



回答2:


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();
                }
            }
        }



回答3:


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




回答4:


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 :)




回答5:


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)
        {

        }



回答6:


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.




回答7:


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



来源:https://stackoverflow.com/questions/4263002/how-to-get-file-name-from-uri

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!