问题描述
我正在为我的Android应用程序使用glide库.
I'm using the glide library for my Android app.
我想告诉它在放弃并显示错误的占位符图像之前,重试X次图像(可能具有指数补偿!).
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.
任何想法如何做到这一点? (我正在使用Volley集成).
Any idea how to do this? (I'm using the Volley integration btw).
推荐答案
使用您自己的ResourceDecoder.我仅加载本地文件,并处理decode()
中的重试计数.如果您使用其他模型,只需更改为适当的界面即可.
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.
Glide 4.0.0-SNAPSHOT的示例.自定义ResourceDecoder:
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;
}
}
必需的自定义ModelLoader
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;
}
}
注册您的自定义模块
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));
}
}
添加到mainfest
Add to mainfest
<application>
...
<meta-data
android:name="com.fileloader.glide.CustomGlideModule"
android:value="GlideModule" />
</application>
这篇关于如何使用Android Glide加载图像指定重试次数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!