Android - Google Drive Api: 如何知道上传任务何时完成?
Android - Google Drive Api: how to know when upload task is completed?
我可以上传很多文件到云端硬盘,但我不知道任务何时完成。
结果回调给我确定,但如果我关闭互联网连接,任务就会中断。这是我正在使用的代码:
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 驱动器 Android API。您使用 GDAA 创建的任何驱动器文件都在设备上创建。一旦创建它们,您就可以删除原始文件。一段时间后,GDAA 会将 Drive File 与云端同步。这最后一个方面对您来说是不可见的,可以忽略。
我可以上传很多文件到云端硬盘,但我不知道任务何时完成。 结果回调给我确定,但如果我关闭互联网连接,任务就会中断。这是我正在使用的代码:
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 驱动器 Android API。您使用 GDAA 创建的任何驱动器文件都在设备上创建。一旦创建它们,您就可以删除原始文件。一段时间后,GDAA 会将 Drive File 与云端同步。这最后一个方面对您来说是不可见的,可以忽略。