我正在尝试通过以mvp模式使用Picasso和rxjava2下载位图。我没有恢复位图,用户照片没有更新。
型号:
override fun downloadPhoto(url: String): Single<Bitmap> = Single.create {
try {
if (!it.isDisposed) {
val bitmap: Bitmap = Picasso.get().load(url).get()
it.onSuccess(bitmap)
}
} catch (e: Throwable) {
it.onError(e)
}
}
演示者
override fun getPhoto() {
auth.rxGetCurrentUser().subscribe { url = it.photoUrl.toString() }
dashboardFragmentModel.downloadPhoto(url).subscribe ({ it -> v.setUserPhoto(it)},
{error -> Log.d("Photo download error", error.message)})
}
查看(片段) }
override fun setUserPhoto(bitmap: Bitmap) {
rootView.userPhoto.setImageBitmap(bitmap)
}
答案 0 :(得分:1)
当您尝试使用url
方法时,也许downloadPhoto
变量未初始化。
尝试一下:
override fun downloadPhoto(url: String): Observable<Bitmap> = Observable.fromCallable {
return@fromCallable Picasso.get().load(url).get()
}
override fun getPhoto() {
auth.rxGetCurrentUser()
.flatMap {
val url = it.photoUrl.toString()
return@flatMap dashboardFragmentModel.downloadPhoto(url)
.subscribeOn(Schedulers.io())
}
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{ it -> v.setUserPhoto(it) },
{ error -> Log.d("Photo download error", error.message) })
}