2012-11-27 52 views
13

Cách kiểm tra tiến trình tải lên sử dụng API GoogleDrive?Kiểm tra tiến trình tải lên và tải xuống (API Google Drive dành cho Android hoặc Java)

service.files(). Insert (body, mediaContent) .execute(); chỉ trả lại tệp mà chúng tôi có thể kiểm tra xem tệp đã tải lên hoàn toàn chưa.

Nhưng tôi cần phải kiểm tra tiến độ trong thời gian thực (ít nhất mỗi giây hoặc lâu hơn), dù sao để làm điều đó?

Và đối với phần tải xuống, tôi nghĩ chúng tôi có thể so sánh số lượng byte mà chúng tôi đã đọc từ luồng đầu vào đến tổng số tệp, theo cách thủ công, sau đó tính toán tiến trình hiện tại.

Nhưng đây có phải là cách chính xác không? Hoặc có cách nào khác để kiểm tra tiến trình tốt hơn?

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: Sử dụng mã này 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

tôi có thể nhận được một số tiến bộ bây giờ nếu tôi đang sử dụng tải lên tiếp nối, tôi đặt chunksize 1MB. NHƯNG hiện có 2 vấn đề.

1) Tải lên có thể tiếp tục chậm hơn nhiều so với tải lên một lần, bởi vì nó dừng nhiều lần ở giữa, chỉ gửi 1MB mỗi lần.
Nếu tôi đặt chunksize thành cao hơn, sau đó nó sẽ không hiển thị tiến trình nào cả cho tệp nhỏ hơn.
Vì vậy, tiến trình tải lên có thể tiếp tục không phải là những gì tôi thực sự muốn. Tôi muốn tiến bộ trong một lần tải lên.

2) Nếu tôi đặt kích thước khoảng 1MB, tải lên của tôi luôn không thành công ở mức khoảng 70% cho tệp 10MB bình thường.
Nếu tôi đặt khối thành 10MB thì nó sẽ thành công, nhưng tôi sẽ không thấy tiến bộ nào. Lỗi

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) 
+0

hiện đang hoạt động NẾU tôi đặt kích thước nhân với 2, ví dụ: 10 * 1024 * 1024. Trước đây tôi gặp lỗi vì tôi đang sử dụng 1000000. – tcboy88

Trả lời

9

Progress nghe:

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

biết thêm thông tin ở đây: 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).

Bởi vì kiến ​​trúc của thư viện, bạn phải chọn giữa đoạn và tiến trình HOẶC một bước mà không cần tiến bộ. Kích thước Chunk nhỏ nhất sẽ cải thiện mọi thứ. Đoạn nhỏ hơn có thể:

setChunkSize(MediaHttpUploader.MINIMUM_CHUNK_SIZE) 

Hy vọng điều đó sẽ hữu ích!

+0

Không thể tìm thấy Giao diện này 'MediaHttpUploaderProgressListener' trong Android. –

+0

Cảm ơn bạn đã xác nhận bản năng của mình về việc tải lên không được phân đoạn không hỗ trợ phản hồi tiến độ – Ian

2

Đối với kích thước đoạn văn, nó phải là bội số của MediaHttpUploader.MINIMUM_CHUNK_SIZE, đó là lý do tại sao nó bị lỗi với giá trị trước đó của bạn.

0

Cách tiếp cận hơi khác với SDK CloudRail thay vì SDK ban đầu.ProgressInputStream sau đây có thể được sử dụng cho chỉ dẫn tiến trình tải lên và tải xuống.

class ProgressInputStream extends InputStream { 

    private InputStream stream; 
    private ProgressListener progressListener; 
    private long offset = 0; 

    public ProgressInputStream(InputStream stream, UploadProgressListener progressListener) { 
    this.stream = stream; 
    this.progressListener = progressListener; 
    } 

    @Override 
    public int read() throws IOException { 
    int res = this.stream.read(); 
    this.progressListener.onProgressChanged(++this.offset); 

    return res; 
    } 

    @Override 
    public int read(byte[] b, int off, int len) throws IOException { 
    int res = this.stream.read(b, off, len); 
    this.offset += res; 
    this.progressListener.onProgressChanged(this.offset); 

    return res; 
    } 

    // You might need to override additional methods but you can just 
    // call the corresponding method on the stream property 

    public interface ProgressListener { 
    void onProgressChanged(long bytes); 
    } 
} 

Hướng dẫn đầy đủ có thể được tìm thấy here.

Các vấn đề liên quan