检查上传和放大器的进展情况;下载(谷歌驱动器API对于Android或Java)放大器、驱动器、进展情况、上传

2023-09-05 11:15:38 作者:海盐布丁

如何检查上传的使用GoogleDrive API的进展如何?

在的 service.files()插入(身体,mediaContent).execute(); 的仅返回的的文件的,我们可以检查文件是否已完全上传

但我需要实时检查进度(至少每秒左右),也会这么做吧?

和下载的部分,我想我们可以手动比较多少字节,我们已经从InputStream总文件大小读,然后计算出当前的进度。

但是,这是正确的做法?或者有检查进度别的更好的办法?

 私人无效saveFileToDrive(){
    线程t =新主题(新的Runnable(){
      @覆盖
      公共无效的run(){
        尝试 {
          //文件的二进制内容
          java.io.File的fileContent =新的java.io.File(fileUri.getPath());
          FileContent mediaContent =新FileContent(图像/ JPEG,fileContent);

          //文件的元数据。
          文件主体=新的文件();
          body.setTitle(fileContent.getName());
          body.setMimeType(为image / jpeg);

          文件fil​​e = service.files()插入(身体,mediaContent).execute()。
          如果(文件!= NULL){
            showToast(照片上载:+ file.getTitle());
            // startCameraIntent();
          }
        }赶上(UserRecoverableAuthIOException E){
          startActivityForResult(e.getIntent(),REQUEST_AUTHORIZATION);
        }赶上(IOException异常E){
          e.printStackTrace();
        }
      }
    });
    t.start();
  }
 

编辑:使用此code http://$c$c.google.com/p/google-api-java-client/source/browse/drive-cmdline-sample/src/main/java/com/google/api/services/samples/drive/cmdline/DriveSample.java?repo=samples&name=based-on-1.12&r=08555cd2a27be66dc97505e15c60853f47d84b5a

我可以现在获得了一些进展,如果我使用的的可恢复上传的,我设置块大小为1MB。 但现在有2个问题。

1)可恢复上传比单个上传慢得多,因为它停下很多次之间,每次只发送1MB。 如果我设置块大小更高,那么它不会显示进度在所有的小文件。 所以,可恢复上传进度是不是我真正想要的。我想在单一的上传进度。

2)如果我设置CHUNKSIZE 1MB左右,我总是上传作为一个正常的10MB文件失败在70%左​​右。 如果我设置块大小为10MB,然后它会成功,但我不会看到任何进展。

  Drive.Files.Insert插入= service.files()插入(身体,mediaContent)。
MediaHttpUploader上传= insert.getMediaHttpUploader();
uploader.setDirectUploadEnabled(假);
uploader.setChunkSize(10 * 1024 * 1024); // previously我用百万这就是为什么它不会工作
uploader.setProgressListener(新FileUploadProgressListener());
com.google.api.services.drive.model.File F = insert.execute();
 
英国新冠死亡人数成全球第二 疫情追踪APP上线,可检测身边是否有人感染新冠

LogCat中错误:

  11-27 19:23:26.927:D / FileUploadProgressListener(11527):正在上传:0.5235538​​030501893
11-27 19:23:33.692:D / FileUploadProgressListener(11527):正在上传:0.56095050326806
11-27 19:23:38.294:D / FileUploadProgressListener(11527):正在上传:0.5983472034859306
11-27 19:23:50.583:D / FileUploadProgressListener(11527):正在上传:0.6357439037038013
11-27 19:23:55.091:D / FileUploadProgressListener(11527):正在上传:0.673140603921672
11-27 19:24:05.130:D / FileUploadProgressListener(11527):正在上传:0.7105373041395426
11-27 19:24:17.005:D / FileUploadProgressListener(11527):正在上传:0.7479340043574133
11-27 19:24:28.888:D / FileUploadProgressListener(11527):正在上传:0.785330704575284
11-27 19:24:28.896:W / HttpTransport(11527):除了在执行请求抛出
11-27 19:24:28.896:W / HttpTransport(11527):java.io.IOException异常:流意外结束
11-27 19:24:28.896:W / HttpTransport(11527):在libcore.net.http.FixedLengthOutputStream.close(FixedLengthOutputStream.java:58)
11-27 19:24:28.896:W / HttpTransport(11527):在com.google.api.client.http.javanet.NetHtt prequest.execute(NetHtt prequest.java:88)
11-27 19:24:28.896:W / HttpTransport(11527):在com.google.api.client.http.Htt prequest.execute(Htt的prequest.java:980)
11-27 19:24:28.896:W / HttpTransport(11527):在com.google.api.client.googleapis.media.MediaHttpUploader.upload(MediaHttpUploader.java:293)
11-27 19:24:28.896:W / HttpTransport(11527):在com.google.api.services.drive.Drive $文件$ Insert.executeUnparsed(Drive.java:309)
11-27 19:24:28.896:W / HttpTransport(11527):在com.google.api.services.drive.Drive $文件$ Insert.execute(Drive.java:331)
 

解决方案

进展监听器:

 私有类FileUploadProgressListener实现MediaHttpUploaderProgressListener {

    私人字符串mFileUploadedName;

    公共FileUploadProgressListener(字符串文件名){
        mFileUploadedName =文件名;
    }

    @覆盖
    公共无效progressChanged(MediaHttpUploader mediaHttpUploader)抛出IOException异常{
        如果(mediaHttpUploader == NULL)回报;
        开关(mediaHttpUploader.getUploadState()){
            案例INITIATION_STARTED:
            //System.out.println("Initiation已经开始)!;
                打破;
            案例INITIATION_COMPLETE:
            //System.out.println("Initiation完成)!;
                打破;
            案例MEDIA_IN_PROGRESS:
                双百分号= mediaHttpUploader.getProgress()* 100;
                如果(Ln.DEBUG){
                    Log.d(Ln.TAG,上传到收存箱:+ mFileUploadedName + - +将String.valueOf(百分比)+%);
                }
                notif.setProgress(百分比,mFileUploadedName).fire();
                打破;
            案例MEDIA_COMPLETE:
            //System.out.println("Upload完成)!;
        }
    }
}
 

这里更多的信息: https://$c$c.google.com/p/google-api-java-client/wiki/MediaUpload

  

直接媒体上传会上传​​整个媒体内容的在一个请求作为反对可恢复媒体上传协议,可以上传在多个请求。

     

做一个直接上传减少HTTP请求的数量,但   增加失败与大型媒体上传的变化(如:   连接故障)。

由于库架构的

,你有块和进步还是一步不进步之间进行选择。 最小的块大小应该改善的东西。 更小的块可能的:

  setChunkSize(MediaHttpUploader.MINIMUM_CHUNK_SIZE)
 

希望它能帮助!

How to check the progress of an upload that use GoogleDrive API?

The service.files().insert(body, mediaContent).execute(); only return a file which we can check if the file has upload completely.

But I need to check the progress in real time(at least each second or so), anyway to do it?

And for download part, I think we can manually compare how many bytes we have already read from the inputstream to the total filesize, then calculate the current progress.

But is this the correct way? Or there is other better way of checking progress?

private void saveFileToDrive() {
    Thread t = new Thread(new Runnable() {
      @Override
      public void run() {
        try {
          // File's binary content
          java.io.File fileContent = new java.io.File(fileUri.getPath());
          FileContent mediaContent = new FileContent("image/jpeg", fileContent);

          // File's metadata.
          File body = new File();
          body.setTitle(fileContent.getName());
          body.setMimeType("image/jpeg");

          File file = service.files().insert(body, mediaContent).execute();
          if (file != null) {
            showToast("Photo uploaded: " + file.getTitle());
            //startCameraIntent();
          }
        } catch (UserRecoverableAuthIOException e) {
          startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION);
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    });
    t.start();
  }

EDITED: Using this code http://code.google.com/p/google-api-java-client/source/browse/drive-cmdline-sample/src/main/java/com/google/api/services/samples/drive/cmdline/DriveSample.java?repo=samples&name=based-on-1.12&r=08555cd2a27be66dc97505e15c60853f47d84b5a

I am able to get some progress now if I am using resumable upload, I set chunksize to 1MB. BUT there are 2 problems now.

1) The resumable upload is much slower than single upload, because it stop many times in between, only sending 1MB each time. If I set chunksize to higher, then it wont show progress at all for smaller file. So, progress in resumable upload is not what I actually want. I want progress in single upload.

2) If I set chunksize about 1MB, my upload always fail at about 70% for a normal 10MB file. If I set chunksize to 10MB then it will success, but I wont see any progress.

Drive.Files.Insert insert = service.files().insert(body, mediaContent);
MediaHttpUploader uploader = insert.getMediaHttpUploader();
uploader.setDirectUploadEnabled(false);
uploader.setChunkSize(10*1024*1024); // previously I am using 1000000 thats why it won't work
uploader.setProgressListener(new FileUploadProgressListener());
com.google.api.services.drive.model.File f = insert.execute();

LogCat Error:

11-27 19:23:26.927: D/FileUploadProgressListener(11527): Upload is In Progress: 0.5235538030501893
11-27 19:23:33.692: D/FileUploadProgressListener(11527): Upload is In Progress: 0.56095050326806
11-27 19:23:38.294: D/FileUploadProgressListener(11527): Upload is In Progress: 0.5983472034859306
11-27 19:23:50.583: D/FileUploadProgressListener(11527): Upload is In Progress: 0.6357439037038013
11-27 19:23:55.091: D/FileUploadProgressListener(11527): Upload is In Progress: 0.673140603921672
11-27 19:24:05.130: D/FileUploadProgressListener(11527): Upload is In Progress: 0.7105373041395426
11-27 19:24:17.005: D/FileUploadProgressListener(11527): Upload is In Progress: 0.7479340043574133
11-27 19:24:28.888: D/FileUploadProgressListener(11527): Upload is In Progress: 0.785330704575284
11-27 19:24:28.896: W/HttpTransport(11527): exception thrown while executing request
11-27 19:24:28.896: W/HttpTransport(11527): java.io.IOException: unexpected end of stream
11-27 19:24:28.896: W/HttpTransport(11527):     at libcore.net.http.FixedLengthOutputStream.close(FixedLengthOutputStream.java:58)
11-27 19:24:28.896: W/HttpTransport(11527):     at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:88)
11-27 19:24:28.896: W/HttpTransport(11527):     at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:980)
11-27 19:24:28.896: W/HttpTransport(11527):     at com.google.api.client.googleapis.media.MediaHttpUploader.upload(MediaHttpUploader.java:293)
11-27 19:24:28.896: W/HttpTransport(11527):     at com.google.api.services.drive.Drive$Files$Insert.executeUnparsed(Drive.java:309)
11-27 19:24:28.896: W/HttpTransport(11527):     at com.google.api.services.drive.Drive$Files$Insert.execute(Drive.java:331)

解决方案

Progress listener:

private class FileUploadProgressListener implements MediaHttpUploaderProgressListener {

    private String mFileUploadedName;

    public FileUploadProgressListener(String fileName) {
        mFileUploadedName = fileName;
    }

    @Override
    public void progressChanged(MediaHttpUploader mediaHttpUploader) throws IOException {
        if (mediaHttpUploader == null) return;
        switch (mediaHttpUploader.getUploadState()) {
            case INITIATION_STARTED:
            //System.out.println("Initiation has started!");
                break;
            case INITIATION_COMPLETE:
            //System.out.println("Initiation is complete!");
                break;
            case MEDIA_IN_PROGRESS:
                double percent = mediaHttpUploader.getProgress() * 100;
                if (Ln.DEBUG) {
                    Log.d(Ln.TAG, "Upload to Dropbox: " + mFileUploadedName + " - " + String.valueOf(percent) + "%");
                }
                notif.setProgress(percent, mFileUploadedName).fire();
                break;
            case MEDIA_COMPLETE:
            //System.out.println("Upload is complete!");
        }
    }
}

More information here: https://code.google.com/p/google-api-java-client/wiki/MediaUpload

Direct media upload will upload the whole media content in one request as opposed to the resumable media upload protocol that could upload in multiple requests.

Doing a direct upload reduces the number of HTTP requests but increases the change of failures with a large media upload (e.g. connection failure).

Because of the architecture of the library, you have to choose between chunk and progress OR one step without progress. The smallest Chunk size should improve things. Smaller chunk possible:

setChunkSize(MediaHttpUploader.MINIMUM_CHUNK_SIZE)

Hope it helps!