我正在我的一个Android应用程序中实施Google云端硬盘。我可以进行身份验证。
在此方法中,我想列出所有文件和文件夹的元数据。
Drive.DriveApi.fetchDriveId(DemoUtil.getGoogleApiClient(), <drive_id>)
.setResultCallback(idCallback)
但这不起作用。它需要驱动器ID,从哪里我将获得此驱动器ID? 我可以获取Google云端硬盘的所有文件和文件夹的元数据列表吗?
答案 0 :(得分:4)
首先,请注意使用GDAA(播放服务的API&#39;)只会为您提供Android应用创建的文件,文件夹,因为它仅支持&#39; SCOPE_FILE&#39;范围。如果这样,你可以使用这样的结构:
ArrayList<ContentValues> contvals = search(null, null, null);
...
private static GoogleApiClient mGAC;
/**************************************************
* find file/folder in GOODrive
* @param prnId parent ID (optional), null searches full drive, "root" searches Drive root
* @param titl file/folder name (optional)
* @param mime file/folder mime type (optional)
* @return arraylist of found objects
*/
static ArrayList<ContentValues> search(String prnId, String titl, String mime) {
ArrayList<ContentValues> gfs = new ArrayList<>();
if (mGAC != null && mGAC.isConnected()) try {
// add query conditions, build query
ArrayList<Filter> fltrs = new ArrayList<>();
if (prnId != null){
fltrs.add(Filters.in(SearchableField.PARENTS,
prnId.equalsIgnoreCase("root") ?
Drive.DriveApi.getRootFolder(mGAC).getDriveId() : DriveId.decodeFromString(prnId)));
}
if (titl != null) fltrs.add(Filters.eq(SearchableField.TITLE, titl));
if (mime != null) fltrs.add(Filters.eq(SearchableField.MIME_TYPE, mime));
Query qry = new Query.Builder().addFilter(Filters.and(fltrs)).build();
// fire the query
MetadataBufferResult rslt = Drive.DriveApi.query(mGAC, qry).await();
if (rslt.getStatus().isSuccess()) {
MetadataBuffer mdb = null;
try {
mdb = rslt.getMetadataBuffer();
for (Metadata md : mdb) {
if (md == null || !md.isDataValid() || md.isTrashed()) continue;
gfs.add(UT.newCVs(md.getTitle(), md.getDriveId().encodeToString()));
}
} finally { if (mdb != null) mdb.close(); }
}
} catch (Exception e) { }
return gfs;
}
只需修改您的ContentValues contvals ,即可保存所需的元数据。代码段取自this demo,您可以在其中找到未解决的背景信息。
如果您需要所有文件/文件夹,无论创建它们的应用是什么,您都必须使用&#39; DriveScopes.DRIVE&#39;来获取REST API。范围,相同的代码片段如下所示:
ArrayList<ContentValues> contvals = search(null, null, null);
...
private static Drive mGOOSvc;
/***************************************************************************
* find file/folder in GOODrive
* @param prnId parent ID (optional), null searches full drive, "root" searches Drive root
* @param titl file/folder name (optional)
* @param mime file/folder mime type (optional)
* @return arraylist of found objects
*/
static ArrayList<ContentValues> search(String prnId, String titl, String mime) {
ArrayList<ContentValues> gfs = new ArrayList<>();
if (mGOOSvc != null && mConnected) try {
// add query conditions, build query
String qryClause = "'me' in owners and ";
if (prnId != null) qryClause += "'" + prnId + "' in parents and ";
if (titl != null) qryClause += "title = '" + titl + "' and ";
if (mime != null) qryClause += "mimeType = '" + mime + "' and ";
qryClause = qryClause.substring(0, qryClause.length() - " and ".length());
Drive.Files.List qry = mGOOSvc.files().list().setQ(qryClause)
.setFields("items(id,mimeType,labels/trashed,title),nextPageToken");
String npTok = null;
if (qry != null) do {
FileList gLst = qry.execute();
if (gLst != null) {
for (File gFl : gLst.getItems()) {
if (gFl.getLabels().getTrashed()) continue;
gfs.add( UT.newCVs(gFl.getTitle(),gFl.getId()));
} //else UT.lg("failed " + gFl.getTitle());
npTok = gLst.getNextPageToken();
qry.setPageToken(npTok);
}
} while (npTok != null && npTok.length() > 0); //UT.lg("found " + vlss.size());
} catch (Exception e) { UT.le(e); }
return gfs;
}
取自here。
祝你好运。