How to get file name from file path in android

后端 未结 10 2561
清歌不尽
清歌不尽 2020-12-08 06:13

I want to get file name from sdcard file path. e.g. :/storage/sdcard0/DCIM/Camera/1414240995236.jpg I want get 1414240995236.jpg I have written the code to fe

相关标签:
10条回答
  • 2020-12-08 06:41

    The easiest solution is to use Uri.getLastPathSegment():

    String filename = uri.getLastPathSegment();
    
    0 讨论(0)
  • 2020-12-08 06:43

    I think you can use substring method to get name of the file from the path string.

    String path=":/storage/sdcard0/DCIM/Camera/1414240995236.jpg"; 
    // it contains your image path...I'm using a temp string...
    String filename=path.substring(path.lastIndexOf("/")+1);
    
    0 讨论(0)
  • 2020-12-08 06:43

    Other Way is:

    String[] parts = selectedFilePath.split("/");
        final String fileName = parts[parts.length-1];
    
    0 讨论(0)
  • 2020-12-08 06:46

    Simple and easy way to get File name

    File file = new File("/storage/sdcard0/DCIM/Camera/1414240995236.jpg"); 
    String strFileName = file.getName();
    

    After add this code and print strFileName you will get strFileName = 1414240995236.jpg

    0 讨论(0)
  • 2020-12-08 06:57

    FilenameUtils to the rescue:

    String filename = FilenameUtils.getName("/storage/sdcard0/DCIM/Camera/1414240995236.jpg");
    
    0 讨论(0)
  • 2020-12-08 06:58

    We can find file name below code:

    File file =new File(Path);
    String filename=file.getName();
    
    0 讨论(0)
提交回复
热议问题