Query for list of files and folders in root directory

后端 未结 3 1467
我寻月下人不归
我寻月下人不归 2021-02-05 14:46

I would like to get a list of files and folders in the root directory without having to sort through all the files. Is there a query that would do this?

相关标签:
3条回答
  • 2021-02-05 15:06

    Google drive v3 Reference the documentation helps to create DriveClient object.

    Run this below method in the background thread(Android).

    Note: Required Scope permission "DriveScopes.DRIVE"

     protected String[] getListChildren(String parentId) throws Exception {
        String[] children = null;
       parentId = parentId == null ? "root" : parentId;
        String fileQuery = "'" + parentId + "' in parents and trashed=false";
        FileList files = driveService.files().list().setQ(fileQuery).execute();
        List<String> fileNames = new ArrayList<String>();
        for (File file : files.getFiles()) {
            fileNames.add(file.getName());
        }
        children = fileNames.toArray(new String[0]);
        return children;
    }
    
    0 讨论(0)
  • 2021-02-05 15:15

    The root folder can also be addressed with a special alias named "root", so you can get all files and folders in the root with the following query:

    https://www.googleapis.com/drive/v2/files?q='root' in parents
    

    Remember to escape the URL if not using one of the client libraries (they automatically take care of it).

    For more details about the search query language, check https://developers.google.com/drive/search-parameters

    0 讨论(0)
  • 2021-02-05 15:21

    This code will display all files and folder of your ROOT DIRECTORY. just copy and paste this code and you will get all your root's file and folder.

     List<File> result = new ArrayList<File>();
        Files.List request = null;
    
        try {
              request = mService.files().list();
              FileList files = request.setQ("'root' in parents and trashed=false").execute();
              result.addAll(files.getItems());          
              request.setPageToken(files.getNextPageToken());
            } 
          catch (IOException e)
          {
            System.out.println("An error occurred: " + e);
            request.setPageToken(null);
          }
    
     //Print out all the files and folder of root Directory  
      for(File f:result)
      {
          System.out.println("recvd data are: "+f.getTitle());
      }
    
    0 讨论(0)
提交回复
热议问题