首页 > 解决方案 > GLIDE:加载为复合可绘制对象时如何处理加载错误?

问题描述

基本上,我正在尝试使用循环转换加载图像并作为CompoundDrawable使用Glide。通常,对于非 CompoundDrawable 的情况,我们可以通过正确配置 RequestOptions 来优雅地处理加载错误,如下所示:

RequestOptions options = new RequestOptions().error(R.drawable.error_image)

但是,当加载到 TextView 作为CompoundDrawable. 以下是我的工具和代码:

Android Studio Gradle plugin = 3.1.2
Gradle version = 4.4
Glide version = 4.7.1
Support Library version = 27.1.1


RequestOptions options = new RequestOptions()
                    .error(R.drawable.error_image)
                    .fitCenter()
                    .circleCrop();

Glide.with(context)
     .setDefaultRequestOptions(options)
     .load(sandwich.getImage())
     .into(new SimpleTarget<Drawable>(100,100) {
         @Override
          public void onResourceReady(@NonNull Drawable resource,
                                      @Nullable Transition<? super Drawable> transition) {
                            sandwichQuickInfo.setCompoundDrawablesWithIntrinsicBounds(
                                    resource,
                                    null,
                                    null,
                                    null
                            );
                        }
                    });

下面是我得到的返回 404 作为支持数据的 url 的错误。

Glide  W  Load failed for some url... [197x197]
       W  class com.bumptech.glide.load.engine.GlideException: Failed to load resource
       W  There were 2 causes:
       W  java.io.FileNotFoundException(some url...)
       W  java.io.FileNotFoundException(No content provider: some url...)
       W  call GlideException#logRootCauses(String) for more detail
       W  Cause (1 of 2): 
              class com.bumptech.glide.load.engine.GlideException: Fetching data failed, class java.io.InputStream, REMOTE
       W      There was 1 cause:java.io.FileNotFoundException(some url...)
       W      call GlideException#logRootCauses(String) for more detail
       W  Cause (1 of 1): class com.bumptech.glide.load.engine.GlideException: Fetch failed
       W      There was 1 cause:java.io.FileNotFoundException(some url...)
       W      call GlideException#logRootCauses(String) for more detail
       W  Cause (1 of 1): class java.io.FileNotFoundException: some url...
       W  Cause (2 of 2): class com.bumptech.glide.load.engine.GlideException: Fetching data failed, class android.content.res.AssetFileDescriptor, LOCAL
       W  There was 1 cause:
       W      java.io.FileNotFoundException(No content provider: some url...)
       W      call GlideException#logRootCauses(String) for more detail
       W  Cause (1 of 1): class java.io.FileNotFoundException: No content provider: some url...


       I  Root cause (1 of 2)
       I  java.io.FileNotFoundException: some url...
       I      at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:251)
       I      at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)
       I      at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(Unknown Source:0)
       I      at com.bumptech.glide.load.data.HttpUrlFetcher.loadDataWithRedirects(HttpUrlFetcher.java:106)
       I      at com.bumptech.glide.load.data.HttpUrlFetcher.loadData(HttpUrlFetcher.java:59)
       I      at com.bumptech.glide.load.model.MultiModelLoader$MultiFetcher.loadData(MultiModelLoader.java:99)
       I      at com.bumptech.glide.load.model.MultiModelLoader$MultiFetcher.startNextOrFail(MultiModelLoader.java:150)
       I      at com.bumptech.glide.load.model.MultiModelLoader$MultiFetcher.onLoadFailed(MultiModelLoader.java:144)
       I      at com.bumptech.glide.load.data.HttpUrlFetcher.loadData(HttpUrlFetcher.java:65)
       I      at com.bumptech.glide.load.model.MultiModelLoader$MultiFetcher.loadData(MultiModelLoader.java:99)
       I      at com.bumptech.glide.load.engine.SourceGenerator.startNext(SourceGenerator.java:62)
       I      at com.bumptech.glide.load.engine.DecodeJob.runGenerators(DecodeJob.java:302)
       I      at com.bumptech.glide.load.engine.DecodeJob.runWrapped(DecodeJob.java:272)
       I      at com.bumptech.glide.load.engine.DecodeJob.run(DecodeJob.java:233)
       I      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
       I      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
       I      at java.lang.Thread.run(Thread.java:764)
       I      at com.bumptech.glide.load.engine.executor.GlideExecutor$DefaultThreadFactory$1.run(GlideExecutor.java:446)
       I  Root cause (2 of 2)
       I  java.io.FileNotFoundException: No content provider: some url...
       I      at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1396)
       I      at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1247)
       I      at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1170)
       I      at com.bumptech.glide.load.data.AssetFileDescriptorLocalUriFetcher.loadResource(AssetFileDescriptorLocalUriFetcher.java:22)
       I      at com.bumptech.glide.load.data.AssetFileDescriptorLocalUriFetcher.loadResource(AssetFileDescriptorLocalUriFetcher.java:13)
       I      at com.bumptech.glide.load.data.LocalUriFetcher.loadData(LocalUriFetcher.java:44)
       I      at com.bumptech.glide.load.engine.SourceGenerator.startNext(SourceGenerator.java:62)
       I      at com.bumptech.glide.load.engine.DecodeJob.runGenerators(DecodeJob.java:302)
       I      at com.bumptech.glide.load.engine.DecodeJob.onDataFetcherFailed(DecodeJob.java:397)
       I      at com.bumptech.glide.load.engine.SourceGenerator.onLoadFailed(SourceGenerator.java:119)
       I      at com.bumptech.glide.load.model.MultiModelLoader$MultiFetcher.startNextOrFail(MultiModelLoader.java:153)
       I      at com.bumptech.glide.load.model.MultiModelLoader$MultiFetcher.onLoadFailed(MultiModelLoader.java:144)
       I      at com.bumptech.glide.load.data.HttpUrlFetcher.loadData(HttpUrlFetcher.java:65)
       I      at com.bumptech.glide.load.model.MultiModelLoader$MultiFetcher.loadData(MultiModelLoader.java:99)
       I      at com.bumptech.glide.load.model.MultiModelLoader$MultiFetcher.startNextOrFail(MultiModelLoader.java:150)
       I      at com.bumptech.glide.load.model.MultiModelLoader$MultiFetcher.onLoadFailed(MultiModelLoader.java:144)
       I      at com.bumptech.glide.load.data.HttpUrlFetcher.loadData(HttpUrlFetcher.java:65)
       I      at com.bumptech.glide.load.model.MultiModelLoader$MultiFetcher.loadData(MultiModelLoader.java:99)
       I      at com.bumptech.glide.load.engine.SourceGenerator.startNext(SourceGenerator.java:62)
       I      at com.bumptech.glide.load.engine.DecodeJob.runGenerators(DecodeJob.java:302)
       I      at com.bumptech.glide.load.engine.DecodeJob.runWrapped(DecodeJob.java:272)
       I      at com.bumptech.glide.load.engine.DecodeJob.run(DecodeJob.java:233)
       I      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
       I      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
       I      at java.lang.Thread.run(Thread.java:764)
       I      at com.bumptech.glide.load.engine.executor.GlideExecutor$DefaultThreadFactory$1.run(GlideExecutor.java:446)

标签: androidandroid-glide

解决方案


推荐阅读