Check if directory exist on android's sdcard

前端 未结 6 715
时光说笑
时光说笑 2020-12-24 05:01

How do I check if a directory exist on the sdcard in android?

6条回答
  •  旧时难觅i
    2020-12-24 05:29

    General use this function for checking is a Dir exists:

    public boolean dir_exists(String dir_path)
      {
        boolean ret = false;
        File dir = new File(dir_path);
        if(dir.exists() && dir.isDirectory())
          ret = true;
        return ret;
      }
    

    Use the Function like:

    String dir_path = Environment.getExternalStorageDirectory() + "//mydirectory//";
    
    if (!dir_exists(dir_path)){
      File directory = new File(dir_path); 
      directory.mkdirs(); 
    }
    
    if (dir_exists(dir_path)){
      // 'Dir exists'
    }else{
    // Display Errormessage 'Dir could not creat!!'
    }
    

提交回复
热议问题