无法在Google云端硬盘Android SDK中上传或下载进度

时间:2016-07-28 07:16:33

标签: android google-drive-api

我正在处理应用程序,我会将某些用户数据备份到Google驱动器,稍后我会将其恢复。

问题是创建和恢复文件没有任何问题,除了我看不到任何进展,如果我上传一个大文件,它继续在后台做,并不能通知用户有一些操作发生在后台。

这是我正在使用的方法的片段

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());在彼此之后很快被调用,实际文件会在后台继续上传。

那么有人能告诉我如何在后台获取上传文件的真实状态吗?

1 个答案:

答案 0 :(得分:1)

您需要添加可以收听下载或上传进度的进度监听器。

要上传,您可以使用Implementation details中提供的MediaHttpUploaderProgressListener实施

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
    }
});

这些SO帖子中提供的解决方案 - Check progress for Upload & Download (Google Drive API for Android or Java)How to show uploading to Google Drive progress in my android App?也可能有所帮助。