我需要通过给定的网址下载图片(位图)。当我在我的项目中使用Glide来显示图片时,我认为我可以简单地使用他们的机制来下载图片。我需要在RxJava环境中使用它,我的问题是没有调用回调onResourceReady(),当然也没有调用subscriber.onNext()和subscriber.onCompleted()方法。
我收到IllegalStateException(见下文)。我想对Glide的调用需要在主线程而不是调用get()的io线程上完成。有没有办法实现这个目标?
这就是id所做的:
public Observable<Bitmap> get(final String url) {
return Observable.create(new Observable.OnSubscribe<Bitmap>() {
@Override
public void call(final Subscriber<? super Bitmap> subscriber) {
Glide
.with(context)
.asBitmap()
.load(url)
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
// the method onResourceReady is not getting called
subscriber.onNext(resource);
subscriber.onCompleted();
}
});
}
});
}
java.lang.IllegalArgumentException:您必须在主线程上调用此方法 09-13 14:24:58.173 25581 25581 W System.err:at com.bumptech.glide.util.Util.assertMainThread(Util.java:132) 09-13 14:24:58.173 25581 25581 W System.err:at com.bumptech.glide.RequestBuilder.into(RequestBuilder.java:350) 09-13 14:24:58.176 25581 25581 W System.err:at de.dumont.bob10.data.repository.image.ImageCloud $ 2.call(ImageCloud.java:53) 09-13 14:24:58.178 25581 25581 W System.err:at de.dumont.bob10.data.repository.image.ImageCloud $ 2.call(ImageCloud.java:44) 09-13 14:24:58.179 25581 25581 W System.err:at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:50) 09-13 14:24:58.180 25581 25581 W System.err:at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30) 09-13 14:24:58.181 25581 25581 W System.err:at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:50) 09-13 14:24:58.182 25581 25581 W System.err:at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30) 09-13 14:24:58.183 25581 25581 W System.err:at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:50) 09-13 14:24:58.183 25581 25581 W System.err:at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30) 09-13 14:24:58.184 25581 25581 W System.err:at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:50) 09-13 14:24:58.185 25581 25581 W System.err:at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30) 09-13 14:24:58.186 25581 25581 W System.err:at rx.Observable.unsafeSubscribe(Observable.java:8666) 09-13 14:24:58.187 25581 25581 W System.err:at rx.internal.operators.OperatorSwitchIfEmpty $ ParentSubscriber.subscribeToAlternate(OperatorSwitchIfEmpty.java:79) 09-13 14:24:58.188 25581 25581 W System.err:at rx.internal.operators.OperatorSwitchIfEmpty $ ParentSubscriber.onCompleted(OperatorSwitchIfEmpty.java:72) 09-13 14:24:58.188 25581 25581 W System.err:at rx.internal.operators.OperatorSwitchIfEmpty $ AlternateSubscriber.onCompleted(OperatorSwitchIfEmpty.java:112) 09-13 14:24:58.189 25581 25581 W System.err:at rx.internal.operators.OperatorMerge $ MergeSubscriber.emitLoop(OperatorMerge.java:650) 09-13 14:24:58.190 25581 25581 W System.err:at rx.internal.operators.OperatorMerge $ MergeSubscriber.emit(OperatorMerge.java:562) 09-13 14:24:58.191 25581 25581 W System.err:at rx.internal.operators.OperatorMerge $ MergeSubscriber.onCompleted(OperatorMerge.java:283) 09-13 14:24:58.192 25581 25581 W System.err:at rx.internal.operators.OperatorMap $ MapSubscriber.onCompleted(OperatorMap.java:94)
答案 0 :(得分:0)
我修好了如下。不幸的是我没有得到位图,但现在对我来说还不错。
public Completable get(final String url) {
return Completable.create(new CompletableOnSubscribe() {
@Override
public void subscribe(CompletableEmitter emitter) throws Exception {
Glide
.with(context)
.load(url)
.downloadOnly(2000, 2000);
emitter.onComplete();
}
});
}