2016-07-28 71 views
0

我正在處理某些用戶數據備份到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());非常迅速對方後稱,並與實際文件上傳保存在背景中。

那麼誰能告訴我如何在後臺獲取上傳文件的真實狀態?

回答

1

您需要添加進度偵聽程序,它們將偵聽下載或上載進度。

對於上傳,你可以使用給出的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?也會有所幫助。