public void rxJavaDownLoadImageAction(){
//起点
Observable.just("https://www.wanandroid.com/blogimgs/62c1bd68-b5f3-4a3c-a649-7ca8c7dfabe6.png")
//需求,下载图片
.map(new Function<String, Bitmap>() {
@Override
public Bitmap apply(String s) throws Throwable {
URL url = new URL("https://www.wanandroid.com/blogimgs/62c1bd68-b5f3-4a3c-a649-7ca8c7dfabe6.png");
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setConnectTimeout(5000);
int responseCode = httpURLConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK){
InputStream inputStream = httpURLConnection.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
return bitmap;
}
return null;
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
new Observer<Bitmap>() {
@Override
public void onSubscribe(@NonNull Disposable d) {
//加载框
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setTitle("正在加载");
progressDialog.show();
}
@Override
public void onNext(@NonNull Bitmap bitmap) {
imageView.setImageBitmap(bitmap);
}
@Override
public void onError(@NonNull Throwable e) {
}
@Override
public void onComplete() {
if (progressDialog !=null){
progressDialog.dismiss();
}
}
}
);
}