Get the directory from a file path in java (android)

前端 未结 4 1666
无人及你
无人及你 2020-12-25 12:16

Is there a function to get the directory part of a file path?

so from

String a=\"/root/sdcard/Pictures/img0001.jpg\";
4条回答
  •  余生分开走
    2020-12-25 12:58

    A better way, use getParent() from File Class..

    String a="/root/sdcard/Pictures/img0001.jpg"; // A valid file path 
    File file = new File(a); 
    String getDirectoryPath = file.getParent(); // Only return path if physical file exist else return null
    

    http://developer.android.com/reference/java/io/File.html#getParent%28%29

提交回复
热议问题