如果Glide失败,如何重试图像加载?

时间:2018-07-30 13:42:53

标签: android android-glide loadimage

我正在使用此代码从网络加载图像。

由于错误FileNotFound,我同时发出多个请求,但是文件实际上在服务器上。

因此,我想问一下,如果滑行失败,如何重试滑行请求?

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

我研究了How to retry image loading ,if Glide fails?的讨论,但没有帮助。

错误日志

  

com.bumptech.glide.load.engine.GlideException类:加载失败   资源       原因有1:       java.io.FileNotFoundException(无内容提供者:http://10.0.2.2:5000/image/tmpfhpbjdsf.png)        致电GlideException#logRootCauses(String)了解更多详细信息         原因(1之1):com.bumptech.glide.load.engine.GlideException类:提取数据失败,   android.content.res.AssetFileDescriptor类,LOCAL,   DataCacheKey {sourceKey = http://10.0.2.2:5000/image/tmpfhpbjdsf.png,   签名= EmptySignature}       原因有1:       java.io.FileNotFoundException(无内容提供者:http://10.0.2.2:5000/image/tmpfhpbjdsf.png)        致电GlideException#logRootCauses(String)了解更多详细信息           原因(1之1):类java.io.FileNotFoundException:无内容提供者:http://10.0.2.2:5000/image/tmpfhpbjdsf.png 07-30   20:51:35.127 18903-18903 / com.example.muhammadusman.project I / Glide:   根本原因(1之1)       java.io.FileNotFoundException:无内容提供者:http://10.0.2.2:5000/image/tmpfhpbjdsf.png           在android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1396)           在android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1247)           在android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1170)           在com.bumptech.glide.load.data.AssetFileDescriptorLocalUriFetcher.loadResource(AssetFileDescriptorLocalUriFetcher.java:22)           在com.bumptech.glide.load.data.AssetFileDescriptorLocalUriFetcher.loadResource(AssetFileDescriptorLocalUriFetcher.java:13)           在com.bumptech.glide.load.data.LocalUriFetcher.loadData(LocalUriFetcher.java:44)           在com.bumptech.glide.load.engine.SourceGenerator.startNext(SourceGenerator.java:62)           在com.bumptech.glide.load.engine.DecodeJob.runGenerators(DecodeJob.java:299)           在com.bumptech.glide.load.engine.DecodeJob.runWrapped(DecodeJob.java:269)           在com.bumptech.glide.load.engine.DecodeJob.run(DecodeJob.java:230)           在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)           在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:636)           在java.lang.Thread.run(Thread.java:764)           在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:再次加载07-30 20:51:37.842 1397-1397 /?   W / audio_hw_generic:不能向HAL提供足够的数据   位置58480553,只写了58327920 07-30 20:51:38.041   18903-18903 / com.example.muhammadusman.project W / Glide:加载失败   http://10.0.2.2:5000/image/tmpqklezoyk.png,大小为[368x268]       com.bumptech.glide.load.engine.GlideException类:无法加载资源       原因有1:       java.io.FileNotFoundException(无内容提供者:http://10.0.2.2:5000/image/tmpqklezoyk.png)        致电GlideException#logRootCauses(String)了解更多详细信息         原因(1之1):com.bumptech.glide.load.engine.GlideException类:提取数据失败,   android.content.res.AssetFileDescriptor类,LOCAL,   DataCacheKey {sourceKey = http://10.0.2.2:5000/image/tmpqklezoyk.png,   签名= EmptySignature}       原因有1:       java.io.FileNotFoundException(无内容提供者:http://10.0.2.2:5000/image/tmpqklezoyk.png)        致电GlideException#logRootCauses(String)了解更多详细信息           原因(1之1):类java.io.FileNotFoundException:无内容提供者:http://10.0.2.2:5000/image/tmpqklezoyk.png 07-30   20:51:38.041 18903-18903 / com.example.muhammadusman.project I / Glide:   根本原因(1之1)       java.io.FileNotFoundException:无内容提供者:http://10.0.2.2:5000/image/tmpqklezoyk.png           在android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1396)           在android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1247)           在android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1170)           在com.bumptech.glide.load.data.AssetFileDescriptorLocalUriFetcher.loadResource(AssetFileDescriptorLocalUriFetcher.java:22)           在com.bumptech.glide.load.data.AssetFileDescriptorLocalUriFetcher.loadResource(AssetFileDescriptorLocalUriFetcher.java:13)           在com.bumptech.glide.load.data.LocalUriFetcher.loadData(LocalUriFetcher.java:44)           在com.bumptech.glide.load.engine.SourceGenerator.startNext(SourceGenerator.java:62)           在com.bumptech.glide.load.engine.DecodeJob.runGenerators(DecodeJob.java:299)           在com.bumptech.glide.load.engine.DecodeJob.runWrapped(DecodeJob.java:269)           在com.bumptech.glide.load.engine.DecodeJob.run(DecodeJob.java:230)           在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)           在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:636)           在java.lang.Thread.run(Thread.java:764)

1 个答案:

答案 0 :(得分:1)

这是图书馆Weak Handler

1。将此库添加到build.gradle文件中。

2。代码如下。

代码

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

注意 如果您的图片加载失败,则此代码将在1 MiiliSecond失败后发送请求。仅在您说资源存在的情况下,这才对您而言效果最佳。

警告 但是,如果资源不存在,那么这不是最佳解决方案。因为递归将无限进行。