使用rxJava通过Glide下载图像 [英] Download Image via Glide with rxJava

查看:67
本文介绍了使用rxJava通过Glide下载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过给定的URL下载图片(位图).当我在项目中使用Glide来显示图片时,我以为我也可以简单地使用它们的机制来下载图片.我需要在RxJava环境中使用它,我的问题是没有调用回调onResourceReady(),当然也没有调用subscriber.onNext()和subscriber.onCompleted()方法.

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.

我得到一个IllegalStateException(见下文).我猜对Glide的调用需要在主线程上完成,而不是在将要调用get()的io线程上进行.有没有办法做到这一点?

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?

这是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)

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)

推荐答案

我已如下修复.不幸的是,我没有找回位图,但这对我来说现在还可以.

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

这篇关于使用rxJava通过Glide下载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆