我可以将许多文件上传到云端硬盘,但我不知道任务何时完成。结果回调给了我正常,但是如果我关闭互联网连接,那么任务就会中断。这是我正在使用的代码:
void createFile(DriveFolder pFldr, final String titl, final String mime, final File file) {
DriveId dId = null;
setProgressing(true);
if (mGoogleApiClient != null && mGoogleApiClient.isConnected() && titl != null && mime != null && file != null) try {
final DriveFolder parent = pFldr != null ? pFldr : Drive.DriveApi.getRootFolder(mGoogleApiClient);
Drive.DriveApi.newDriveContents(getGoogleApiClient()).setResultCallback(new ResultCallback<DriveApi.DriveContentsResult>() {
@Override public void onResult(DriveApi.DriveContentsResult driveContentsResult) {
DriveContents cont = driveContentsResult != null && driveContentsResult.getStatus().isSuccess() ?
driveContentsResult.getDriveContents() : null;
if (cont != null) try {
OutputStream oos = cont.getOutputStream();
if (oos != null) try {
InputStream is = new FileInputStream(file);
byte[] buf = new byte[4096];
int c;
while ((c = is.read(buf, 0, buf.length)) > 0) {
oos.write(buf, 0, c);
oos.flush();
}
}
finally { oos.close();}
MetadataChangeSet meta = new MetadataChangeSet.Builder().setTitle(titl).setMimeType(mime).build();
parent.createFile(mGoogleApiClient, meta, cont).setResultCallback(new ResultCallback<DriveFolder.DriveFileResult>() {
@Override public void onResult(DriveFolder.DriveFileResult driveFileResult) {
DriveFile dFil = driveFileResult != null && driveFileResult.getStatus().isSuccess() ?
driveFileResult.getDriveFile() : null;
if (dFil != null) {
Log.d(TAG,"photo "+count+" uploaded" );
count --;
if(count == 0){
// is the task completed?
}
} else {
Log.d(TAG,"error during upload photo " + count );
}
}
});
} catch (Exception e) {
e.printStackTrace();
ShowErrorHelper.showErrorDialog(UploadActivity.this, e);
}
}
});
} catch (Exception e) {
e.printStackTrace();
ShowErrorHelper.showErrorDialog(UploadActivity.this, e);
}
}
我想知道何时确定所有文件都已上传到云(以便我可以在本地删除它们(。
您正在使用 Google Drive Android API。您使用 GDAA 创建的所有云端硬盘文件都会在设备上创建。创建后,您可以立即删除原始文件。一段时间后,GDAA 会将云端硬盘文件与云端同步。最后一个方面对你来说是不可见的,可以忽略。