无法上传或下载Google Drive Android SDK中的进度
问题描述:
我正在处理某些用户数据备份到Google驱动器的应用程序,并在稍后时间恢复它们。无法上传或下载Google Drive Android SDK中的进度
问题是,创建和恢复的文件没有问题,除了我看不到任何进展,如果我上传一个大文件,它一直在后台执行该操作,并且无法通知用户存在一些操作发生在后台。
这里是从方法的一个片段,我使用
Drive.DriveApi.newDriveContents(client)
.setResultCallback(new ResultCallback<DriveApi.DriveContentsResult>() {
@Override
public void onResult(@NonNull DriveApi.DriveContentsResult driveContentsResult) {
final DriveContents driveContents = driveContentsResult.getDriveContents();
File file = new File(filesToUpload.get(0).getURI());
// write content to DriveContents
OutputStream outputStream = driveContentsResult.getDriveContents().getOutputStream();
try {
outputStream.write(FileManagerUtils.getBytes(file));
} catch (IOException e) {
e.printStackTrace();
NotificationManger.dismissUploadingNotification();
NotificationManger.showSucessNotification(getApplicationContext(), R.string.notification_uploading_success);
}
MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
.setTitle(obj.getFileName())
.build();
DriveId folderID = null;
// create a file on root folder
Drive.DriveApi.getFolder(client, folderID)
.createFile(client, changeSet, driveContents)
.setResultCallback(new ResultCallbacks<DriveFolder.DriveFileResult>() {
@Override
public void onSuccess(@NonNull DriveFolder.DriveFileResult result) {
if (!result.getStatus().isSuccess()) {
Log.d(TAG, "Error while trying to create the file");
return;
}
Log.d(TAG, "Created a file with content: " + result.getDriveFile().getDriveId());
if (filesToUpload.size() > 0) {
filesToUpload.remove(0);
backup();
}
}
@Override
public void onFailure(@NonNull Status status) {
// show error
}
});
}
});
的问题是,如果我'上传3个文件,该 Log.d(TAG,Log.d(TAG, "Created a file with content: " + result.getDriveFile().getDriveId());
非常迅速对方后称,并与实际文件上传保存在背景中。
那么谁能告诉我如何在后台获取上传文件的真实状态?
答
您需要添加进度侦听程序,它们将侦听下载或上载进度。
对于上传,你可以使用给出的MediaHttpUploaderProgressListener
实施Implementation details
public static class MyUploadProgressListener implements MediaHttpUploaderProgressListener {
public void progressChanged(MediaHttpUploader uploader) throws IOException {
switch (uploader.getUploadState()) {
case INITIATION_STARTED:
System.out.println("Initiation Started");
break;
case INITIATION_COMPLETE:
System.out.println("Initiation Completed");
break;
case MEDIA_IN_PROGRESS:
System.out.println("Upload in progress");
System.out.println("Upload percentage: " + uploader.getProgress());
break;
case MEDIA_COMPLETE:
System.out.println("Upload Completed!");
break;
}
}
}
对于下载,您可以附加DownloadProgressListener
通知的下载进度的用户在ProgressDialog
。如Opening the file contents所示,特别是在侦听下载进度时,使用DownloadProgressListener打开文件内容。在这些所谓后给出
file.open(mGoogleClientApi, DriveFile.MODE_READ_ONLY, new DownloadProgressListener() {
@Override
public void onProgress(long bytesDownloaded, long bytesExpected) {
// display the progress
}
});
解决方案 - Check progress for Upload & Download (Google Drive API for Android or Java)和How to show uploading to Google Drive progress in my android App?也会有所帮助。