How to retry image loading, if Glide fails?
Asked Answered
N

1

8

I am using this code to load the image from network.

I am making multiple request at the same time due to which the error comes FileNotFound but the file actually exist on server.

Therefore i want to ask that how can i retry the glide request if glide fails?

 public static void LoadNetworkImage(final Context context, final ImageView imageView, final String imageUrl) {

        Glide.with(context).
                load(imageUrl).
                listener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {

                      //if loading fails then again make request

                        LoadNetworkImage(context,imageView,imageUrl);
                        return true;
                    }

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

i have studied this discussion How to retry image loading ,if Glide fails? but it did not help.

Error Logs

class com.bumptech.glide.load.engine.GlideException: Failed to load resource There was 1 cause: java.io.FileNotFoundException(No content provider: http://10.0.2.2:5000/image/tmpfhpbjdsf.png) call GlideException#logRootCauses(String) for more detail Cause (1 of 1): class com.bumptech.glide.load.engine.GlideException: Fetching data failed, class android.content.res.AssetFileDescriptor, LOCAL, DataCacheKey{sourceKey=http://10.0.2.2:5000/image/tmpfhpbjdsf.png, signature=EmptySignature} There was 1 cause: java.io.FileNotFoundException(No content provider: http://10.0.2.2:5000/image/tmpfhpbjdsf.png) call GlideException#logRootCauses(String) for more detail Cause (1 of 1): class java.io.FileNotFoundException: No content provider: http://10.0.2.2:5000/image/tmpfhpbjdsf.png 07-30 20:51:35.127 18903-18903/com.example.muhammadusman.project I/Glide: Root cause (1 of 1) java.io.FileNotFoundException: No content provider: http://10.0.2.2:5000/image/tmpfhpbjdsf.png at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1396) at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1247) at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1170) at com.bumptech.glide.load.data.AssetFileDescriptorLocalUriFetcher.loadResource(AssetFileDescriptorLocalUriFetcher.java:22) at com.bumptech.glide.load.data.AssetFileDescriptorLocalUriFetcher.loadResource(AssetFileDescriptorLocalUriFetcher.java:13) at com.bumptech.glide.load.data.LocalUriFetcher.loadData(LocalUriFetcher.java:44) at com.bumptech.glide.load.engine.SourceGenerator.startNext(SourceGenerator.java:62) at com.bumptech.glide.load.engine.DecodeJob.runGenerators(DecodeJob.java:299) at com.bumptech.glide.load.engine.DecodeJob.runWrapped(DecodeJob.java:269) at com.bumptech.glide.load.engine.DecodeJob.run(DecodeJob.java:230) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636) at java.lang.Thread.run(Thread.java:764) at com.bumptech.glide.load.engine.executor.GlideExecutor$DefaultThreadFactory$1.run(GlideExecutor.java:446) 07-30 20:51:35.127 18903-18903/com.example.muhammadusman.project D/MyMessage: Again loading 07-30 20:51:37.842 1397-1397/? W/audio_hw_generic: Not supplying enough data to HAL, expected position 58480553 , only wrote 58327920 07-30 20:51:38.041 18903-18903/com.example.muhammadusman.project W/Glide: Load failed for http://10.0.2.2:5000/image/tmpqklezoyk.png with size [368x268] class com.bumptech.glide.load.engine.GlideException: Failed to load resource There was 1 cause: java.io.FileNotFoundException(No content provider: http://10.0.2.2:5000/image/tmpqklezoyk.png) call GlideException#logRootCauses(String) for more detail Cause (1 of 1): class com.bumptech.glide.load.engine.GlideException: Fetching data failed, class android.content.res.AssetFileDescriptor, LOCAL, DataCacheKey{sourceKey=http://10.0.2.2:5000/image/tmpqklezoyk.png, signature=EmptySignature} There was 1 cause: java.io.FileNotFoundException(No content provider: http://10.0.2.2:5000/image/tmpqklezoyk.png) call GlideException#logRootCauses(String) for more detail Cause (1 of 1): class java.io.FileNotFoundException: No content provider: http://10.0.2.2:5000/image/tmpqklezoyk.png 07-30 20:51:38.041 18903-18903/com.example.muhammadusman.project I/Glide: Root cause (1 of 1) java.io.FileNotFoundException: No content provider: http://10.0.2.2:5000/image/tmpqklezoyk.png at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1396) at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1247) at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1170) at com.bumptech.glide.load.data.AssetFileDescriptorLocalUriFetcher.loadResource(AssetFileDescriptorLocalUriFetcher.java:22) at com.bumptech.glide.load.data.AssetFileDescriptorLocalUriFetcher.loadResource(AssetFileDescriptorLocalUriFetcher.java:13) at com.bumptech.glide.load.data.LocalUriFetcher.loadData(LocalUriFetcher.java:44) at com.bumptech.glide.load.engine.SourceGenerator.startNext(SourceGenerator.java:62) at com.bumptech.glide.load.engine.DecodeJob.runGenerators(DecodeJob.java:299) at com.bumptech.glide.load.engine.DecodeJob.runWrapped(DecodeJob.java:269) at com.bumptech.glide.load.engine.DecodeJob.run(DecodeJob.java:230) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636) at java.lang.Thread.run(Thread.java:764)

Naucratis answered 30/7, 2018 at 13:42 Comment(3)
#35463584Disable
#38251302Disable
show your log and server side code?Ademption
A
4

This is the library Weak Handler

1.Add this library into the build.gradle file.

2.Code is given below.

Code

 public static void LoadNetworkImage(final Context context, final ImageView imageView, final String imageUrl) {



        final WeakHandler mHandler = new WeakHandler();
        final Runnable runnable = new Runnable() {
            @Override
            public void run() {
                LoadNetworkImage(context, imageView,imageUrl);
            }
        };

        Glide.with(context).
                load(imageUrl).
                transition(DrawableTransitionOptions.withCrossFade()).
                listener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {


                        mHandler.postDelayed(runnable,1);
                        return true;
                    }

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

Note if your image loading fails then this code will sent the request after 1 MiiliSecond of failure. This will work best in your case only because you have said that the resource exist.

Warning But if the resource does not exist then this is not the best solution. Because the recursion will go infinitely.

Ademption answered 30/7, 2018 at 18:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.