java - Rxjava2, Retrofit2, download image as byte array -


i try download several image url address. after decode byte[] bitmap , set imageview. code works perfect.

request request = new request.builder()                             .url(imageurl)                             .build();                      new okhttpclient().newcall(request).enqueue(new callback() {                         @override                         public void onfailure(call call, ioexception e) {}                          @override                         public void onresponse(call call, response response) throws ioexception {                             try {                                 bytearrayoutputstream outputstream = new bytearrayoutputstream();                                 int current;                                 while ((current = response.body().bytestream().read()) != -1) {                                     outputstream.write((byte) current);                                 }                                 byte[] array = outputstream.tobytearray();                                 ..........                             }                         }                     }); 

but if use rxjava2, , retrofit2 doesn't work out. byte array come out many 0 values.

public interface imageapi {  @get observable<responsebody> requestimage(@url string utl);}    observable(imageurl()).subscribe(new observer<responsebody>() {                         @override                         public void onsubscribe(@nonnull disposable d) {}                          @override                         public void onnext(@nonnull responsebody responsebody) {                             try {                                 if (responsebody != null && responsebody.bytes() != null) {                                     bytearrayoutputstream outputstream = new bytearrayoutputstream();                                     int current;                                     while ((current = responsebody.bytestream().read()) != -1) {                                         outputstream.write((byte) current);                                     }                                     byte[] array = outputstream.tobytearray();                                  }                         } 

what doing wrong ? difference between okhttpclient request , retrofit2 ?

p.s. don't need use glide or picasso.

responsebody.bytes() != null - call bytes() reads entire stream , closes response. trying access responsebody.bytestream() afterwards returns closed stream.

the correct code be:

byte[] array = responsebody.bytes(); 

since responsebody cannot null (rxjava2 forbids it) , bytes() returns value or throws exception.


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -