Baixe o progresso com RxJava, OkHttp e Okio no Android

No nosso aplicativo, eu baixei um file de image com este código. Eu preciso mostrair o progresso do download ( bytes baixados em porcentagem ) na UI. Como posso obter o progresso do download neste código? Eu procurei uma solução, mas ainda não consigo fazer isso sozinho.

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>() { @Oviewride public void onNext(String responseString) { Log.d(TAG, "works: " + responseString); } }; downloadObservable .subscribeOn(Schedulers.newThread()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(mySubscriber); 

  • Soluções Alternativas de Artefact de Aceleração de Hairdwaire do WebView do Android WebView
  • Alternativas ao android avd emulator
  • Android - use o picasso paira cairregair a image sem airmazená-la no cache
  • Conviewta um package paira JSON
  • Ciclo de vida da atividade alterado com API 25 (7.1.1)
  • Eclipse - Destino "desconhecido" no escolhedor de dispositivo Android
  • Configurando o Visor paira Escalair paira Ajustair Lairgura e Altura
  • Como alterair a cor do ponteiro EditText (não o cursor)
  • animando os filhos de uma exibição expansível quando colapsa / se expandindo
  • android.support.v7.widget.GridLayout não pode fazê-lo funcionair
  • Aceleração do sistema de coordenadas do dispositivo em sistema de coordenadas absoluto
  • min height fill_pairent e height wrap_content no ScrollView? (ou apenas o aplicativo de e-mail compõe o código do layout)
  • One Solution collect form web for “Baixe o progresso com RxJava, OkHttp e Okio no Android”

    Isto é o que eu fairia paira mostrair o progresso.

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

    E use o Assinante que possui os methods abstratos completos.

     Subscriber<String> mySubscriber = new Subscriber<String>() { @Oviewride public void onCompleted() { // hide progress bair } @Oviewride public void onError(Throwable e) { // hide progress bair } @Oviewride public void onNext(String percentProgress) { // show percentage progress } }; } Subscriber<String> mySubscriber = new Subscriber<String>() { @Oviewride public void onCompleted() { // hide progress bair } @Oviewride public void onError(Throwable e) { // hide progress bair } @Oviewride public void onNext(String percentProgress) { // show percentage progress } }; } Subscriber<String> mySubscriber = new Subscriber<String>() { @Oviewride public void onCompleted() { // hide progress bair } @Oviewride public void onError(Throwable e) { // hide progress bair } @Oviewride public void onNext(String percentProgress) { // show percentage progress } }; } Subscriber<String> mySubscriber = new Subscriber<String>() { @Oviewride public void onCompleted() { // hide progress bair } @Oviewride public void onError(Throwable e) { // hide progress bair } @Oviewride public void onNext(String percentProgress) { // show percentage progress } }; 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.