在Android中使用glide和Rxjava从url与其他Api调用并行获取位图 [英] Get bitmap from url with other Api calls parallelly using glide and Rxjava in Android

查看:59
本文介绍了在Android中使用glide和Rxjava从url与其他Api调用并行获取位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过以下方式并行调用两个 API.

I am making two API calls parallel in the following way.

Single.zip(API1.subscribeOn(Schedulers.io()),
                API2.subscribeOn(Schedulers.io()), Bifunction())).subscribe();

我想从 URL 加载位图并将位图发回.像这样

I would want to load a bitmap from an URL and send back the bitmap. Something like this

Single.zip(API1.subscribeOn(Schedulers.io()),
                    API2.subscribeOn(Schedulers.io()), getImageBitmap(), Function3())).subscribe();

在方法 getImageBitmap 中,我试图返回 Single.像这样的东西.

In the method getImageBitmap I am trying to return Single<Bitmap>. Something like this.

Single.create(new SingleOnSubscribe<Bitmap>() {
            @Override
            public void subscribe(final SingleEmitter<Bitmap> emitter) throws Exception {
                Glide.with(context).asBitmap().load(url)
                        .diskCacheStrategy(DiskCacheStrategy.NONE)
                        .error(R.drawable.ic_login_error).addListener(new RequestListener<Bitmap>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {
                        emitter.onSuccess(null);
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
                        emitter.onSuccess(resource);
                        return false;
                    }
                });
            }
        }).subscribeOn(AndroidSchedulers.mainThread());

我没有得到任何结果.如何通过以下方式从 Glide 中获取 Bitmap?

I am not getting any results out of this. How to get Bitmap from Glide in the following approach?

推荐答案

当从 Glide 加载图像时,我们应该调用 .submit() 方法或 .into(...) 以便成功获取位图或加载图像.

While loading the image from Glide, we should either call the .submit() method or .into(...) in order to successfully get the bitmap or load the image.

这篇关于在Android中使用glide和Rxjava从url与其他Api调用并行获取位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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