how to get file name from URI

后端 未结 8 595
花落未央
花落未央 2020-12-16 15:46

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

相关标签:
8条回答
  • 2020-12-16 16:04
    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 :)

    0 讨论(0)
  • 2020-12-16 16:08
    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)
            {
    
            }
    
    0 讨论(0)
  • 2020-12-16 16:10

    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();
    
    0 讨论(0)
  • 2020-12-16 16:12

    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();
                    }
                }
            }
    
    0 讨论(0)
  • 2020-12-16 16:12

    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.

    0 讨论(0)
  • 2020-12-16 16:16

    For kotlin just simply:

    val fileName = File(uri.path).name
    
    0 讨论(0)
提交回复
热议问题