Запрос для списка файлов и папок в корневом каталоге
-
13-12-2019 - |
Вопрос
Я хотел бы получить список файлов и папок в корневом каталоге, без необходимости отсортировать все файлы.Есть ли запрос, который бы сделал это?
Решение
Корневая папка также может быть адресована специальным псевдоним под названием «root», поэтому вы можете получить все файлы и папки в корне с следующим запросом:
https://www.googleapis.com/drive/v2/files?q='root' in parents
.
Не забывайте сбежать URL, если не использовать один из клиентских библиотек (они автоматически позаботятся об этом).
Для получения дополнительной информации о языке поискового запроса, проверьте https://developers.google.com/drive/search-parameters
Другие советы
Этот код отобразит все файлы и папку из вашего корневого каталога
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 Ссылка Документация помогает создать DriveClient Object.
Запустите этот метод ниже в фоновом потоке (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;
}
.