久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

檢查上傳和進度下載(適用于 Android 或 Java 的 Go

Check progress for Upload amp; Download (Google Drive API for Android or Java)(檢查上傳和進度下載(適用于 Android 或 Java 的 Google Drive API))
本文介紹了檢查上傳和進度下載(適用于 Android 或 Java 的 Google Drive API)的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

如何查看使用 GoogleDrive API 的上傳進度?

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

service.files().insert(body, mediaContent).execute(); 只返回一個文件我們可以檢查文件是否已完全上傳.

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?

對于下載部分,我認為我們可以手動比較我們已經(jīng)從輸入流中讀取的字節(jié)數(shù)與總文件大小,然后計算當前進度.

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

已使用此代碼 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=基于-1.12&r=08555cd2a27be66dc97505e15c60853f47d84b5a

如果我使用可恢復上傳,我現(xiàn)在能夠取得一些進展,我將塊大小設(shè)置為 1MB.但是現(xiàn)在有兩個問題.

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) 可恢復上傳比單次上傳慢很多,因為中間會停很多次,每次只發(fā)送1MB.
如果我將 chunksize 設(shè)置為更高,那么對于較小的文件,它根本不會顯示進度.
所以,可恢復上傳的進展并不是我真正想要的.我想要單次上傳的進度.

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) 如果我將塊大小設(shè)置為大約 1MB,對于一個正常的 10MB 文件,我的上傳總是失敗約為 70%.
如果我將 chunksize 設(shè)置為 10MB,那么它會成功,但我不會看到任何進展.

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 錯誤:

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)

推薦答案

進度監(jiān)聽器:

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

更多信息在這里:https://code.google.com/p/google-api-java-client/維基/媒體上傳

直接媒體上傳將在一個請求中上傳整個媒體內(nèi)容,而不是可以在多個請求中上傳的可恢復媒體上傳協(xié)議.

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.

直接上傳會減少 HTTP 請求的數(shù)量,但增加失敗的變化與大型媒體上傳(例如連接失敗).

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).

由于庫的架構(gòu),您必須在塊和進度或沒有進度的一步之間進行選擇.最小的塊大小應(yīng)該可以改善事情.可能的更小的塊:

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)

希望對你有幫助!

這篇關(guān)于檢查上傳和進度下載(適用于 Android 或 Java 的 Google Drive API)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Upload progress listener not fired (Google drive API)(上傳進度偵聽器未觸發(fā)(Google 驅(qū)動器 API))
Save file in specific folder with Google Drive SDK(使用 Google Drive SDK 將文件保存在特定文件夾中)
Google Drive Android API - Invalid DriveId and Null ResourceId(Google Drive Android API - 無效的 DriveId 和 Null ResourceId)
Google drive api services account view uploaded files to google drive using java(谷歌驅(qū)動api服務(wù)賬戶查看上傳文件到谷歌驅(qū)動使用java)
Google Drive service account returns 403 usageLimits(Google Drive 服務(wù)帳號返回 403 usageLimits)
com.google.api.client.json.jackson.JacksonFactory; missing in Google Drive example(com.google.api.client.json.jackson.JacksonFactory;Google Drive 示例中缺少)
主站蜘蛛池模板: 免费九九视频 | 欧美精品久久 | 日本啊v在线 | 精品国产乱码久久久久久1区2区 | 久久美女视频 | 超碰520| 日韩在线观看精品 | 久久久久亚洲 | 一区二区在线不卡 | 亚洲国产成人精品女人久久久野战 | 日韩视频一区在线观看 | www.久久99| 成人亚洲在线 | 99视频入口 | 国产高清一区二区 | 亚洲综合大片69999 | 欧美日韩综合精品 | 欧美精品一区二区三区蜜桃视频 | 老司机久久 | 一区二区三区四区在线免费观看 | 黄色91在线 | 久久精品亚洲 | 日韩欧美国产一区二区 | 国产成人精品综合 | 91久久精品 | 一级毛片大全免费播放 | 日本久草 | 午夜视频免费在线观看 | 毛片免费视频 | 中文字幕亚洲国产 | 中文在线一区二区 | 亚洲精品久久久久中文字幕欢迎你 | 夜夜艹| 国产伦一区二区三区久久 | 久久久久久久电影 | 人人艹人人 | 日韩一区二区三区视频在线观看 | 激情在线视频 | 色婷婷一区二区三区四区 | 国产成人在线观看免费 | 久久久久久久综合 |