0

I am working on application where i take a backup of some user's data to Google drive and in a later time i restore them.

The problem is that the files created and restored with no problems except that i can't see any progress, if i am uploading a large file it keep doing that in the background and can't notify the user that there is some operation happening in the background.

here is a snippet from the method i am using

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

The problem is that if i'am uploading 3 files, the Log.d(TAG,Log.d(TAG, "Created a file with content: " + result.getDriveFile().getDriveId()); is called very quickly after each others, and the actual files keep uploading in the background.

So can anyone tell me how to get real status of the uploading files in the background?

1 Answers1

1

You need to add progress listeners which will listen to download or upload progress.

For uploading, you can use the MediaHttpUploaderProgressListener implementation given in 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;
      }
    }
  }

For downloading, you can attach a DownloadProgressListener to inform users of the download progress in a ProgressDialog. As shown in Opening the file contents specifically in listening to the download progress, open the file contents with a DownloadProgressListener.

file.open(mGoogleClientApi, DriveFile.MODE_READ_ONLY, new DownloadProgressListener() {
    @Override
    public void onProgress(long bytesDownloaded, long bytesExpected) {
        // display the progress
    }
});

Solutions given in these SO post - Check progress for Upload & Download (Google Drive API for Android or Java) and How to show uploading to Google Drive progress in my android App? might help too.

Community
  • 1
  • 1
Teyam
  • 7,686
  • 3
  • 15
  • 22