Download Image via Glide with rxJava

爱⌒轻易说出口 提交于 2021-01-28 05:22:09

问题


I need to download pictures (Bitmaps) via a given url. As I use Glide in my project to display the pictures I thought I could simply use their mechanism to download the pictures as well. I need to use it in a RxJava Environment and my problem is that the callback onResourceReady() is not called and of course the methods subscriber.onNext() and subscriber.onCompleted() are not getting called either.

I get an IllegalStateException (see below). I guess the call to Glide needs to be done on the main thread instead of the io thread where get() will be called. Is there a way to achieve this?

This is what id do:

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: You must call this method on the main thread 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)


回答1:


I fixed it like below. Unfortunately I do not get the bitmap back but that is ok-ish for me for the moment.

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();
        }
    });
}


来源:https://stackoverflow.com/questions/46196895/download-image-via-glide-with-rxjava

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!