美文网首页
rxjava2 retrofit2的文件下载与保存

rxjava2 retrofit2的文件下载与保存

作者: 手微凉_ | 来源:发表于2017-11-29 13:11 被阅读0次

保存文件时要使用RxJava的doOnNext、map等操作符,后续更新UI的操作切换到UI线程。主线程中应该避免耗时操作

retrofit.create(ApiService.class)

.dowmBitmap("http://imgcache.qq.com/qzone/space_item/pre/0/66768.gif")

.compose(this.bindToLifecycle())

.subscribeOn(Schedulers.newThread())

.map(newFunction() {

@Override

public String apply(ResponseBody responseBody)throwsException { //返回类型可以自己更改

return FileUtils.saveBitmap(mContext,responseBody.byteStream(),HttpConfig.IMAGE_PATH, null,"gif", false, true);//图片保存 

}

})

.observeOn(AndroidSchedulers.mainThread())

.subscribe(newConsumer() {

@Override

public void accept(String s)throwsException {

if(s !=null&& s.length() >0) {

QQApiRequest.get().shareImage(mContext,s, true);

}else{

T.showMessage(mContext,"gif下载失败");

}

}

});

相关文章

网友评论

      本文标题:rxjava2 retrofit2的文件下载与保存

      本文链接:https://www.haomeiwen.com/subject/wxpubxtx.html