Android - Google Drive Api:如何知道上传任务何时完成?
问题描述:
我可以将多个文件上传到云端硬盘,但我不知道任务何时完成。 结果回调让我很满意,但如果我关闭了互联网连接,那么任务会中断。这是我使用的代码:Android - Google Drive Api:如何知道上传任务何时完成?
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会将云端硬盘文件同步到云端。这最后一个方面对你来说是不可见的,可以忽略。
好的,我可以删除这些文件,谢谢!但是,没有办法确定文件是否已经在云端?例如,我想保持互联网连接打开,直到任务完成。 问题是,如果用户关闭连接并在第二天重新打开,我必须等待一天才能查看云中的文件,并且我需要几乎实时访问它们。 – user2044083
如果您想要达到该级别的实时和确定性行为,请停止使用GDAA并切换到REST API。 – pinoyyid