check image is loaded image(url) by glide

我的未来我决定 提交于 2019-12-24 12:21:49

问题


In my android app i have 4 url for one single image. to check if url 1 is broken then go to url 2 and the same way until the end. i using Glide 4.3.1 for loading image like this :

private int checkAndShow4Image(Context context, View view, int img_id, String img_url) {
    try {            
        GlideApp
                .with(context)
                .load(img_url)
                .diskCacheStrategy(DiskCacheStrategy.DATA)
                .error(R.drawable.ic_broken_image)
                .fallback(R.drawable.ic_broken_image)
                .transition(DrawableTransitionOptions.withCrossFade())
                .into((ImageView) view.findViewById(img_id));

    } catch (Exception e) {
        e.printStackTrace();
        return -1;
    }
    return 0;
}

the method is returning before of complete loading. what is the best way for waiting for do it?


回答1:


Glide use asynchronous way to display the image, try to use a callback to know when the image is completed or failed:

.listener(new RequestListener<Drawable>() {
                                @Override
                                public boolean onLoadFailed(@android.support.annotation.Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {

                                    return false;
                                }

                                @Override
                                public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                                    return false;
                                }
                            }
                    )
                    .into(view);


来源:https://stackoverflow.com/questions/47380419/check-image-is-loaded-imageurl-by-glide

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