gpt4 book ai didi

android-image - 如何正确捕获 Glide 异常?

转载 作者:行者123 更新时间:2023-12-04 13:42:24 35 4
gpt4 key购买 nike

正在使用 Glide 4.x 并在 URL 返回 404 时获取 FileNotFoundException。我尝试使用 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: Load failed for https://lh4.googleusercontent.com/-UxjwFuQRjXA/AAAAAAAAAAI/AAAAAAAAAAA/ACLGyWByUX_zz0_9075kpV4xIdPmN_dtNQ/s96-c/photo.jpg with size [183x183] class com.bumptech.glide.load.engine.GlideException: Failed to load resource There were 2 causes: 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) call GlideException#logRootCauses(String) for more detail Cause (1 of 3): class com.bumptech.glide.load.engine.GlideException: Fetching data failed, class java.io.InputStream, REMOTE There was 1 cause: java.io.FileNotFoundException(https://lh4.googleusercontent.com/-UxjwFuQRjXA/AAAAAAAAAAI/AAAAAAAAAAA/ACLGyWByUX_zz0_9075kpV4xIdPmN_dtNQ/s96-c/photo.jpg) call GlideException#logRootCauses(String) for more detail Cause (1 of 1): class java.io.FileNotFoundException: https://lh4.googleusercontent.com/-UxjwFuQRjXA/AAAAAAAAAAI/AAAAAAAAAAA/ACLGyWByUX_zz0_9075kpV4xIdPmN_dtNQ/s96-c/photo.jpg Cause (2 of 3): class com.bumptech.glide.load.engine.GlideException: Fetching data failed, class java.io.InputStream, REMOTE There was 1 cause: java.io.FileNotFoundException(https://lh4.googleusercontent.com/-UxjwFuQRjXA/AAAAAAAAAAI/AAAAAAAAAAA/ACLGyWByUX_zz0_9075kpV4xIdPmN_dtNQ/s96-c/photo.jpg) call GlideException#logRootCauses(String) for more detail Cause (1 of 1): class java.io.FileNotFoundException: https://lh4.googleusercontent.com/-UxjwFuQRjXA/AAAAAAAAAAI/AAAAAAAAAAA/ACLGyWByUX_zz0_9075kpV4xIdPmN_dtNQ/s96-c/photo.jpg Cause (3 of 3): class com.bumptech.glide.load.engine.GlideException: Failed LoadPath{StringUri->Object->Drawable}, LOCAL Cause (1 of 2): class com.bumptech.glide.load.engine.GlideException: Failed DecodePath{StringUri->Drawable->Drawable} Cause (2 of 2): class com.bumptech.glide.load.engine.GlideException: Failed DecodePath{StringUri->Bitmap->Drawable} 2019-03-20 17:48:07.135 32233-32233/com.zyta.zflikz I/Glide: Root cause (1 of 2) java.io.FileNotFoundException: https://lh4.googleusercontent.com/-UxjwFuQRjXA/AAAAAAAAAAI/AAAAAAAAAAA/ACLGyWByUX_zz0_9075kpV4xIdPmN_dtNQ/s96-c/photo.jpg at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:251) at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210) at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:26) at com.bumptech.glide.load.data.HttpUrlFetcher.loadDataWithRedirects(HttpUrlFetcher.java:106) at com.bumptech.glide.load.data.HttpUrlFetcher.loadData(HttpUrlFetcher.java:59) at com.bumptech.glide.load.engine.SourceGenerator.startNext(SourceGenerator.java:62) at com.bumptech.glide.load.engine.DecodeJob.runGenerators(DecodeJob.java:302) at com.bumptech.glide.load.engine.DecodeJob.runWrapped(DecodeJob.java:272) at com.bumptech.glide.load.engine.DecodeJob.run(DecodeJob.java:233) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) at java.lang.Thread.run(Thread.java:764) at 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: Root cause (2 of 2) java.io.FileNotFoundException: https://lh4.googleusercontent.com/-UxjwFuQRjXA/AAAAAAAAAAI/AAAAAAAAAAA/ACLGyWByUX_zz0_9075kpV4xIdPmN_dtNQ/s96-c/photo.jpg at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:251) at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210) at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:26) at com.bumptech.glide.load.data.HttpUrlFetcher.loadDataWithRedirects(HttpUrlFetcher.java:106) at com.bumptech.glide.load.data.HttpUrlFetcher.loadData(HttpUrlFetcher.java:59) at com.bumptech.glide.load.engine.SourceGenerator.startNext(SourceGenerator.java:62) at com.bumptech.glide.load.engine.DecodeJob.runGenerators(DecodeJob.java:302) at com.bumptech.glide.load.engine.DecodeJob.onDataFetcherFailed(DecodeJob.java:397) at com.bumptech.glide.load.engine.SourceGenerator.onLoadFailed(SourceGenerator.java:119) at com.bumptech.glide.load.data.HttpUrlFetcher.loadData(HttpUrlFetcher.java:65) at com.bumptech.glide.load.engine.SourceGenerator.startNext(SourceGenerator.java:62) at com.bumptech.glide.load.engine.DecodeJob.runGenerators(DecodeJob.java:302) at com.bumptech.glide.load.engine.DecodeJob.runWrapped(DecodeJob.java:272) at com.bumptech.glide.load.engine.DecodeJob.run(DecodeJob.java:233) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) at java.lang.Thread.run(Thread.java:764) at 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.onLoadFailedFailed to load resource



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

最佳答案

此问题已通过在错误方法中使用 Drawable 的本地镜像中的 Glide 进行另一个调用来解决。

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

关于android-image - 如何正确捕获 Glide 异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55260859/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com