Note that there are some explanatory texts on larger screens.

plurals
  1. POCheck progress for Upload & Download (Google Drive API for Android or Java)
    text
    copied!<p>How to check the progress of an upload that use GoogleDrive API?</p> <p>The <strong><em>service.files().insert(body, mediaContent).execute();</em></strong> only return a <strong><em>file</em></strong> which we can check if the file has upload completely.</p> <p>But I need to check the progress in real time(at least each second or so), anyway to do it?</p> <p>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.</p> <p>But is this the correct way? Or there is other better way of checking progress?</p> <pre><code>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(); } </code></pre> <p>EDITED: Using this code <a href="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&amp;name=based-on-1.12&amp;r=08555cd2a27be66dc97505e15c60853f47d84b5a" rel="noreferrer">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&amp;name=based-on-1.12&amp;r=08555cd2a27be66dc97505e15c60853f47d84b5a</a></p> <p>I am able to get some progress now if I am using <strong><em>resumable upload</em></strong>, I set chunksize to 1MB. BUT there are 2 problems now.</p> <p>1) The resumable upload is much slower than single upload, because it stop many times in between, only sending 1MB each time.<br> If I set chunksize to higher, then it wont show progress at all for smaller file.<br> So, progress in resumable upload is not what I actually want. I want progress in single upload.</p> <p>2) If I set chunksize about 1MB, my upload always <strong>fail</strong> at about 70% for a normal 10MB file.<br> If I set chunksize to 10MB then it will success, but I wont see any progress.</p> <pre><code>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(); </code></pre> <p>LogCat Error:</p> <pre><code>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) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload