我正在使用Google Drive Api。我想從Google驅動器下載圖像。 下面是正在工作的代碼,但是當循環結束時調用files.open()。假設count是3,循環將首先完成循環,然後files.open()將被調用3次。正因爲如此,我無法在我的模型類中設置數據。它只需要最後一個值。 (編者)循環中的結果回調(從Google驅動器下載多個圖像。)
if (getGoogleApiClient() != null && getGoogleApiClient().isConnected()) {
try {
final DriveFolder pFldr = Drive.DriveApi.getRootFolder(getGoogleApiClient());
pFldr.listChildren(getGoogleApiClient()).setResultCallback(new ResultCallback<DriveApi.MetadataBufferResult>() {
@Override
public void onResult(DriveApi.MetadataBufferResult metadataBufferResult) {
int count = metadataBufferResult.getMetadataBuffer().getCount();
Log.e("COUNT---", count + "");
MetadataBuffer buffer = metadataBufferResult.getMetadataBuffer();
if (count > 0) {
for (int i = 0; i < count; i++) {
if (buffer.get(i).getMimeType().equals("image/jpeg")) {
Log.e("Images avaialbe - ", "" + buffer.get(i).getTitle());
DriveId id = buffer.get(i).getDriveId();
DriveFile files = Drive.DriveApi.getFile(getGoogleApiClient(), id);
driveModel.setTitle(buffer.get(i).getTitle());
files.open(getGoogleApiClient(), DriveFile.MODE_READ_ONLY, null).setResultCallback(saveImageCallback);
}
if (buffer.get(i).getMimeType().equals("application/x-sqlite3")) {
DriveId id = buffer.get(i).getDriveId();
DriveFile files = Drive.DriveApi.getFile(getGoogleApiClient(), id);
files.open(getGoogleApiClient(), DriveFile.MODE_READ_ONLY, null).setResultCallback(saveDataBaseCallback);
}
}
buffer.release();
showMessage("Success");
} else {
showMessage("No Back Up available.");
}
}
});
} finally {
}
}
ResultCallback<DriveApi.DriveContentsResult> saveImageCallback = new ResultCallback<DriveApi.DriveContentsResult>() {
@Override
public void onResult(DriveApi.DriveContentsResult driveContentsResult) {
DriveContents cont = driveContentsResult.getDriveContents();
InputStream inputStream = cont.getInputStream();
try {
saveFiles(inputStream, driveModel.getTitle(), false);
cont.discard(getGoogleApiClient());
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
如果是第一次捕捉'try-catch'? –
@GürtuğGüngör,這是一個嘗試,最後,沒有趕上。 –
@ OleV.V。,對不起,我編輯時誤刪了,然後當我看着我的編輯版本,我沒有看到它,並寫下了問題。 –