题
我想在根目录中获取一个文件和文件夹列表,而无需排序所有文件。有没有这样做的问题?
解决方案
还可以使用名为“root”的特殊别名来寻址根文件夹,因此您可以使用以下查询获取root中的所有文件和文件夹:
https://www.googleapis.com/drive/v2/files?q='root' in parents
.
如果不使用其中一个客户端库(它们自动处理它),请记住URL。
有关搜索查询语言的更多详细信息,请检查 https://developers.google.com/drive/search-parameters其他提示
此代码将显示根目录的所有文件和文件夹。只需复制并粘贴此代码,您将获取所有root文件和文件夹。
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());
}
. google drive v3 参考文档有助于创建驱动程序。
在背景线程(Android)中运行以下方法。
注意:必需的范围权限“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;
}
. 不隶属于 StackOverflow