Skip to content

Instantly share code, notes, and snippets.

@shts
Created June 24, 2016 08:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shts/47dd80ad2f2b53706e4bb74743fbd459 to your computer and use it in GitHub Desktop.
Save shts/47dd80ad2f2b53706e4bb74743fbd459 to your computer and use it in GitHub Desktop.
RxJavaでファイルダウンロード
public class Download {
public Observable<File> download(List<String> target) {
return Observable.from(target)
.map(new Func1<String, okhttp3.Request>() {
@Override
public okhttp3.Request call(String url) {
return new Request.Builder().url(url).build();
}
})
.map(new Func1<Request, File>() {
@Override
public File call(Request request) {
try {
okhttp3.Response response =
new OkHttpClient.Builder().build().newCall(request).execute();
if (!response.isSuccessful()) throw new IOException("");
Bitmap bitmap = BitmapFactory.decodeStream(response.body().byteStream());
// TODO: bitmap into file
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment