I'm using the glide library for my Android app.
I would like to tell it to retry getting the image X times ( maybe with exponential backoff! ), before giving up and showing the error placeholder image.
Any idea how to do this? (I'm using the Volley integration btw).
Use your own ResourceDecoder. I'm loading local files only and handle the retry count in decode()
. If you use another model just change to the appropriate interface.
Example for Glide 4.0.0-SNAPSHOT. The custom 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;
}
}
Required custom ModelLoader
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;
}
}
Register your custom module
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));
}
}
Add to mainfest
<application>
...
<meta-data
android:name="com.fileloader.glide.CustomGlideModule"
android:value="GlideModule" />
</application>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With