标签:
Returns an Observable that emits the results of a specified combiner function applied to combinations of two items emitted,
in sequence, by two other Observables.
流程图:
简单来说zip操作符就是合并多个数据流,
然后发送(Emit)最终合并的数据。
需求描述:
主要逻辑:
//需要上传的图片
Picture[] ps = xxx;
Observable.zip(
Observable.from(ps),
getUpYunAddress(ps.length),//获取上传的url
new Func2<Picture, UpYunAddress, Picture>() {
@Override
public Picture call(Picture picture, UpYunAddress upYunAddress) {
//如果该图片已经上传则不应该上传
if (TextUtils.isEmpty(picture.getSource())) {
try {
//使用又拍云提供的工具类,上传图片
String path = UpYunUtil.uploadImage(upYunAddress, picture.getLocalUrl());
//获取最终的url
String finalUrl = upYunAddress.getPrefix() + path;
picture.setSource(finalUrl);
} catch (Exception e) {
e.printStackTrace();
}
}
return picture;
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
//上传成功后获取图片大小
.flatMap(new Func1<Picture, Observable<Picture>>() {
@Override
public Observable<Picture> call(Picture picture) {
if (TextUtils.isEmpty(picture.getHeight()) || TextUtils.isEmpty(picture.getWidth())) {
BitmapFactory.Options options;
if (!TextUtils.isEmpty(picture.getLocalUrl())) {
options = ImageUtil.getBitmapOptions(picture.getLocalUrl());
picture.setLocalUrl(null);
} else {
options = ImageUtil.getBitmapOptions(picture.getSource());
}
picture.setWidth(String.valueOf(options.outWidth));
picture.setHeight(String.valueOf(options.outHeight));
}
return Observable.just(picture);
}
});
//最后处理最终的数据。
文章转自:http://blog.csdn.net/johnny901114/article/details/51614927
感谢原作者的分享!
标签:
原文地址:http://blog.csdn.net/jdsjlzx/article/details/51724087