Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check image is loaded image(url) by glide

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?

like image 802
DanialAbdi Avatar asked Nov 19 '17 18:11

DanialAbdi


1 Answers

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);
like image 89
diegoveloper Avatar answered Oct 04 '22 16:10

diegoveloper