【问题标题】:How to specify the number of retries using Glide for Android for loading images?如何指定使用 Glide for Android 加载图像的重试次数?
【发布时间】:2016-02-17 17:02:57
【问题描述】:

我正在为我的 Android 应用程序使用 glide 库。

我想告诉它在放弃并显示错误占位符图像之前重试获取图像 X 次(可能使用指数退避!)。

知道怎么做吗? (顺便说一句,我正在使用 Volley 集成)。

【问题讨论】:

    标签: android android-image android-glide


    【解决方案1】:

    使用您自己的资源解码器。我只加载本地文件并处理decode() 中的重试计数。如果您使用其他模型,只需更改相应的界面即可。

    Glide 4.0.0-SNAPSHOT 的示例。 自定义 ResourceDecoder:

    public class FileDecoder implements ResourceDecoder<File, Drawable>{
        private final Context context;
        private int retryCounter = 0;
    
        public FileDecoder(Context context) {
            this.context = context;
        }
    
        @Override
        public Resource<Drawable> decode(File source, int width, int height, Options options) throws IOException{
    
            source = getTheFile(); //loading the image from a zip
    
            final Drawable icon = Drawable.createFromPath(source.getAbsolutePath());
            if(icon == null){
                if(retryCounter < 3){
                    retryCounter++;
                    return decode(source, width, height, options);
                }
    
                return null;
            }
    
            return new DrawableResource<Drawable>(icon) {
                @Override public Class<Drawable> getResourceClass() {
                    return Drawable.class;
                }
    
                @Override public int getSize() {
                    if (drawable instanceof BitmapDrawable) {
                        return Util.getBitmapByteSize(((BitmapDrawable) drawable).getBitmap());
                    } else {
                        return 1;
                    }
                }
    
                @Override public void recycle() {}
            };
        }
    
        @Override public boolean handles(File source, Options options) throws IOException {
            return true;
        }
    }
    

    必需的自定义模型加载器

    public class FileModelLoader implements ModelLoader<File, File>{
    
        @Nullable @Override
        public LoadData<File> buildLoadData(final File file, int width, int height, Options options){
            return new LoadData<>(new ObjectKey(file), new DataFetcher<File>() {
                @Override
                public void loadData(Priority priority, DataCallback<? super File> callback) {
                    callback.onDataReady(file);
                }
    
                @Override public void cleanup() {
    
                }
    
                @Override public void cancel() {
    
                }
    
                @Override public Class<File> getDataClass() {
                    return File.class;
                }
    
                @Override public DataSource getDataSource() {
                    return DataSource.LOCAL;
                }
            });
        }
    
        @Override public boolean handles(File file){
            return true;
        }
    }
    

    注册您的自定义模块

    public class CustomGlideModule implements GlideModule{
    
        @Override public void applyOptions(Context context, GlideBuilder builder){
            builder.setDefaultRequestOptions(RequestOptions.formatOf(DecodeFormat.PREFER_RGB_565)); //less memory consumption but less quality
        }
    
        @Override public void registerComponents(Context context, Registry registry){
            registry.append(File.class, File.class, new ModelLoaderFactory<File, File>(){
                @Override public ModelLoader<File, File> build(MultiModelLoaderFactory multiFactory){
                    return new FileModelLoader();
                }
    
                @Override public void teardown(){
    
                }
            }).append(File.class, Drawable.class, new FileDecoder(context));
        }
    }
    

    添加到主要节日

    <application>
        ...
    
        <meta-data
        android:name="com.fileloader.glide.CustomGlideModule"
        android:value="GlideModule" />
    
    </application>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-11
      • 1970-01-01
      • 2019-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多