我正在尝试使用播放服务中提供的驱动器sdk从Google驱动器中获取文件列表。
我正在使用google本身在Android Demo中提供的以下查询。
Query query = new Query.Builder()
.build();
Drive.DriveApi.query(mGoogleApiClient, query)
.setResultCallback(metadataCallback);
/**
* Gives the result back in MetadataBufferReuslt format
*/
final private ResultCallback<DriveApi.MetadataBufferResult> metadataCallback =
new ResultCallback<DriveApi.MetadataBufferResult>() {
@Override
public void onResult(DriveApi.MetadataBufferResult result) {
ArrayList<GoogleDriveResultModel> googleDriveResultArr=new ArrayList<>();
if (!result.getStatus().isSuccess()) {
showSnackBar("Problem while retrieving results");
return;
}
googleDriveResultArr.clear();
showSnackBar("Found " + result.getMetadataBuffer().getCount() + " files");
for(Metadata metadata: result.getMetadataBuffer()){
GoogleDriveResultModel model = new GoogleDriveResultModel();
model.setTitle(metadata.getTitle());
model.setGoogleDriveId(metadata.getDriveId().getResourceId());
model.setLink(metadata.getWebContentLink());
googleDriveResultArr.add(model);
Config.debug("Google File " + metadata.getDriveId().getResourceId() + " : " + metadata.getTitle() +
" : " + metadata.getWebContentLink()+" is Trashed "+metadata.isTrashed());
}
}
};
这里的问题是我没有收到我添加或删除的文件。它显示相同的文件。我认为它不与服务器同步。我试图通过Google云端硬盘应用程序查看这些文件,一切似乎都很好。它只是我的应用程序没有获取更新的文件(即添加/删除文件)任何猜测?
答案 0 :(得分:0)
复制此方法并调用它:
private void saveFileToDevice(){
try {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
FileList temp = null;
try {
temp = service
.files()
.list()
.execute();
} catch (UserRecoverableAuthIOException e) {
startActivityForResult(e.getIntent(),
REQUEST_AUTHORIZATION);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (temp != null) {
if (temp.getItems().size() <= 0) {
showToast("No File Available for Download.");
DeleteAlbumTask del = new DeleteAlbumTask();
del.execute(0);
return;
}
final FileList finalTemp = temp;
runOnUiThread(new Runnable() {
@Override
public void run() {
showFileSelectList(finalTemp);
}
});
} else {
showToast("No File Available for Download.");
DeleteAlbumTask del = new DeleteAlbumTask();
del.execute(0);
return;
}
}
});
t.start();
} catch (Exception ex) {
showToast(ex.getMessage());
}
}
答案 1 :(得分:0)
<强>更新强>
根据我们上面的评论交流,下面的解决方案可能没有帮助。 GDAA支持的可见范围很可能出现问题。请参阅&#39; Connecting and Authorizing the Google Drive Android API&#39;段落。
原始回答
如果我没有看到查询本身,很难弄清楚你的查询失败的原因。
因此,由于我无法帮助您,我可能会提供您可能尝试插入的代码段,只是为了看看您是否幸运。您可以修改过滤器构建部分以满足您的需要,它必须位于非UI线程包装器中,如AyncTask,Thread,...
父ID作为DriveId的String表达式提供。当然,mGAC是mGoogleApiClient。
/****************************************************************************
* 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)
*/
static void search(String prnId, String titl, String mime) {
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;
// md.getTitle(), md.getDriveId(), ....
}
} finally { if (mdb != null) mdb.close(); }
}
} catch (Exception e) {}
}
到目前为止,它对我有用,祝你好运