如何正确捕获Glide异常?

时间:2019-03-20 12:31:37

标签: android-image filenotfoundexception android-glide

Am使用Glide 4.x,并且URL返回FileNotFoundException时返回404。我试图使用RequestListener捕获异常。但这不是缓存此异常。

GlideApp.with(getApplicationContext()).load(URL).placeholder(R.mipmap.ic_launcher).listener(new RequestListener<Drawable>() {
                            @Override
                            public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                                System.out.println("MainActivity.onLoadFailed"+e.getMessage());
                                return false;
                            }

                            @Override
                            public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                                System.out.println("MainActivity.onResourceReady");
                                return false;
                            }
                        }).error(R.mipmap.ic_launcher).transform(new CircleCrop()).into(profileImage);

记录如下:

  

2019-03-20 17:48:07.134 32233-32233 / com.zyta.zflikz W / Glide:负载   失败了   https://lh4.googleusercontent.com/-UxjwFuQRjXA/AAAAAAAAAAI/AAAAAAAAAAA/ACLGyWByUX_zz0_9075kpV4xIdPmN_dtNQ/s96-c/photo.jpg   尺寸为[183x183]       com.bumptech.glide.load.engine.GlideException类:无法加载资源       原因有两个:       java.io.FileNotFoundException(https://lh4.googleusercontent.com/-UxjwFuQRjXA/AAAAAAAAAAI/AAAAAAAAAAA/ACLGyWByUX_zz0_9075kpV4xIdPmN_dtNQ/s96-c/photo.jpg)       java.io.FileNotFoundException(https://lh4.googleusercontent.com/-UxjwFuQRjXA/AAAAAAAAAAI/AAAAAAAAAAA/ACLGyWByUX_zz0_9075kpV4xIdPmN_dtNQ/s96-c/photo.jpg)        致电GlideException#logRootCauses(String)了解更多详细信息         原因(3之1):com.bumptech.glide.load.engine.GlideException类:提取数据失败,   类java.io.InputStream,REMOTE       原因有1:       java.io.FileNotFoundException(https://lh4.googleusercontent.com/-UxjwFuQRjXA/AAAAAAAAAAI/AAAAAAAAAAA/ACLGyWByUX_zz0_9075kpV4xIdPmN_dtNQ/s96-c/photo.jpg)        致电GlideException#logRootCauses(String)了解更多详细信息           原因(1之1):类java.io.FileNotFoundException:https://lh4.googleusercontent.com/-UxjwFuQRjXA/AAAAAAAAAAI/AAAAAAAAAAA/ACLGyWByUX_zz0_9075kpV4xIdPmN_dtNQ/s96-c/photo.jpg         原因(2之3):com.bumptech.glide.load.engine.GlideException类:提取数据失败,   类java.io.InputStream,REMOTE       原因有1:       java.io.FileNotFoundException(https://lh4.googleusercontent.com/-UxjwFuQRjXA/AAAAAAAAAAI/AAAAAAAAAAA/ACLGyWByUX_zz0_9075kpV4xIdPmN_dtNQ/s96-c/photo.jpg)        致电GlideException#logRootCauses(String)了解更多详细信息           原因(1之1):类java.io.FileNotFoundException:https://lh4.googleusercontent.com/-UxjwFuQRjXA/AAAAAAAAAAI/AAAAAAAAAAA/ACLGyWByUX_zz0_9075kpV4xIdPmN_dtNQ/s96-c/photo.jpg         原因(3之3):com.bumptech.glide.load.engine.GlideException类:失败   LoadPath {StringUri-> Object-> Drawable},局部           原因(2之1):com.bumptech.glide.load.engine.GlideException类:失败   DecodePath {StringUri-> Drawable-> Drawable}           原因(2之2):com.bumptech.glide.load.engine.GlideException类:失败   DecodePath {StringUri-> Bitmap-> Drawable} 2019-03-20 17:48:07.135   32233-32233 / com.zyta.zflikz I / Glide:根本原因(第1个,共2个)       java.io.FileNotFoundException:https://lh4.googleusercontent.com/-UxjwFuQRjXA/AAAAAAAAAAI/AAAAAAAAAAA/ACLGyWByUX_zz0_9075kpV4xIdPmN_dtNQ/s96-c/photo.jpg           在com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:251)           在com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)           在com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:26)           在com.bumptech.glide.load.data.HttpUrlFetcher.loadDataWithRedirects(HttpUrlFetcher.java:106)           在com.bumptech.glide.load.data.HttpUrlFetcher.loadData(HttpUrlFetcher.java:59)           在com.bumptech.glide.load.engine.SourceGenerator.startNext(SourceGenerator.java:62)           在com.bumptech.glide.load.engine.DecodeJob.runGenerators(DecodeJob.java:302)           在com.bumptech.glide.load.engine.DecodeJob.runWrapped(DecodeJob.java:272)           在com.bumptech.glide.load.engine.DecodeJob.run(DecodeJob.java:233)           在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)           在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:641)           在java.lang.Thread.run(Thread.java:764)           在com.bumptech.glide.load.engine.executor.GlideExecutor $ DefaultThreadFactory $ 1.run(GlideExecutor.java:446)   2019-03-20 17:48:07.137 32233-32233 / com.zyta.zflikz I / Glide:根目录   原因(2之2)       java.io.FileNotFoundException:https://lh4.googleusercontent.com/-UxjwFuQRjXA/AAAAAAAAAAI/AAAAAAAAAAA/ACLGyWByUX_zz0_9075kpV4xIdPmN_dtNQ/s96-c/photo.jpg           在com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:251)           在com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)           在com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:26)           在com.bumptech.glide.load.data.HttpUrlFetcher.loadDataWithRedirects(HttpUrlFetcher.java:106)           在com.bumptech.glide.load.data.HttpUrlFetcher.loadData(HttpUrlFetcher.java:59)           在com.bumptech.glide.load.engine.SourceGenerator.startNext(SourceGenerator.java:62)           在com.bumptech.glide.load.engine.DecodeJob.runGenerators(DecodeJob.java:302)           在com.bumptech.glide.load.engine.DecodeJob.onDataFetcherFailed(DecodeJob.java:397)           在com.bumptech.glide.load.engine.SourceGenerator.onLoadFailed(SourceGenerator.java:119)           在com.bumptech.glide.load.data.HttpUrlFetcher.loadData(HttpUrlFetcher.java:65)           在com.bumptech.glide.load.engine.SourceGenerator.startNext(SourceGenerator.java:62)           在com.bumptech.glide.load.engine.DecodeJob.runGenerators(DecodeJob.java:302)           在com.bumptech.glide.load.engine.DecodeJob.runWrapped(DecodeJob.java:272)           在com.bumptech.glide.load.engine.DecodeJob.run(DecodeJob.java:233)           在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)           在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:641)           在java.lang.Thread.run(Thread.java:764)           在com.bumptech.glide.load.engine.executor.GlideExecutor $ DefaultThreadFactory $ 1.run(GlideExecutor.java:446)   2019-03-20 17:48:07.138 32233-32233 / com.zyta.zflikz I / System.out:   MainActivity.onLoadFailed无法加载资源

请让我知道是否有人为此提供修复或替代解决方案。

1 个答案:

答案 0 :(得分:0)

通过在Drawable中以错误方法在本地图像中用Glide再次调用来解决此问题。

Glide.with(mContext).load(url).error(Glide.with(imgView).load(R.drawable.ic_image_when_url_fails)).into(imgView);