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

前端 未结 4 1655
无人及你
无人及你 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 13:06

    Yes. First, construct a File representing the image path:

    File file = new File(a);
    

    If you're starting from a relative path:

    file = new File(file.getAbsolutePath());
    

    Then, get the parent:

    String dir = file.getParent();
    

    Or, if you want the directory as a File object,

    File dirAsFile = file.getParentFile();
    

提交回复
热议问题