2015-04-30 22 views
12

Trong ứng dụng của chúng tôi, tôi tải xuống tệp hình ảnh có mã này. Tôi cần hiển thị tiến trình tải xuống (byte đã tải xuống theo tỷ lệ phần trăm) trên giao diện người dùng. Làm thế nào tôi có thể tải xuống tiến trình trong mã này? Tôi đã tìm kiếm giải pháp, nhưng vẫn không thể tự mình làm được.Tải xuống tiến trình với RxJava, OkHttp và Okio trong Android

Observable<String> downloadObservable = Observable.create(
        sub -> { 
         Request request = new Request.Builder() 
           .url(media.getMediaUrl()) 
           .build(); 
         Response response = null; 
         try { 
          response = http_client.newCall(request).execute(); 
          if (response.isSuccessful()) { 
           Log.d(TAG, "response.isSuccessful()"); 
           String mimeType = MimeTypeMap.getFileExtensionFromUrl(media.getMediaUrl()); 
           File file = new File(helper.getTmpFolder() + "/" + helper.generateUniqueName() + "test." + mimeType); 
           BufferedSink sink = Okio.buffer(Okio.sink(file)); 
           sink.writeAll(response.body().source()); 
           sink.close(); 
           sub.onNext(response.toString()); 
           sub.onCompleted(); 
          } else { 
           sub.onError(new IOException()); 
          } 
         } catch (IOException e) { 
          e.printStackTrace(); 
         } 

        } 
      ); 

      Subscriber<String> mySubscriber = new Subscriber<String>() { 
       @Override 
       public void onNext(String responseString) { 
        Log.d(TAG, "works: " + responseString); 
       } 
      }; 
      downloadObservable 
        .subscribeOn(Schedulers.newThread()) 
        .observeOn(AndroidSchedulers.mainThread()) 
        .subscribe(mySubscriber); 
+0

tìm thấy ví dụ điển hình liên quan đến câu hỏi của tôi http://danosipov.com/?p=678 –

Trả lời

19

Đây là những gì tôi sẽ làm để hiển thị tiến độ.

Observable<String> downloadObservable = Observable.create(
    sub -> { 
      InputStream input = null; 
      OutputStream output = null; 
      try { 
      Response response = http_client.newCall(request).execute(); 
      if (response.isSuccessful()) {     
      input = response.body().byteStream(); 
      long tlength= response.body().contentLength(); 

      output = new FileOutputStream("/pathtofile"); 
      byte data[] = new byte[1024]; 

      sub.onNext("0%"); 
      long total = 0; 
      int count; 
      while ((count = input.read(data)) != -1) { 
       total += count; 

       sub.onNext(String.valueOf(total*100/tlength) + "%"); 

       output.write(data, 0, count); 
      } 
      output.flush(); 
      output.close(); 
      input.close(); 
      } 
      } catch(IOException e){ 
      sub.onError(e); 
      } finally { 
       if (input != null){ 
        try { 
         input.close(); 
        }catch(IOException ioe){} 
       } 
       if (out != null){ 
        try{ 
         output.close(); 
        }catch (IOException e){}       
       } 
      } 
     sub.onCompleted(); 
    } 
); 

Và sử dụng Người đăng ký có phương pháp trừu tượng hoàn chỉnh.

Subscriber<String> mySubscriber = new Subscriber<String>() { 

@Override 
public void onCompleted() { 
    // hide progress bar 
} 

@Override 
public void onError(Throwable e) { 
    // hide progress bar 
} 

@Override 
public void onNext(String percentProgress) { 
    // show percentage progress 
} 
}; 
+0

tôi có nghĩa là tôi cần thể hiện sự tiến bộ tải theo tỷ lệ –

+2

tôi cập nhật câu trả lời của tôi, nhưng tôi không biết Okio vì vậy tôi có thể 't nói về nó – inmyth

+0

Làm việc như một say mê. Tôi đang sử dụng trang bị thêm và nó đã trở nên khá phức tạp để tải xuống một tệp đơn giản. –

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