如何使用 Glide for Android 指定加载图片的重试次数?
How to specify the number of retries using Glide for Android for loading images?
我正在为我的 Android 应用程序使用 glide 库。
我想告诉它在放弃并显示错误占位符图像之前重试 X 次获取图像(可能使用指数退避!)。
知道怎么做吗? (顺便说一句,我正在使用 Volley 集成)。
使用您自己的 ResourceDecoder。我仅加载本地文件并在 decode()
中处理重试计数。如果您使用其他型号,只需更改为相应的界面即可。
Glide 4.0.0-SNAPSHOT 示例。
自定义资源解码器:
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
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
<application>
...
<meta-data
android:name="com.fileloader.glide.CustomGlideModule"
android:value="GlideModule" />
</application>
我正在为我的 Android 应用程序使用 glide 库。
我想告诉它在放弃并显示错误占位符图像之前重试 X 次获取图像(可能使用指数退避!)。
知道怎么做吗? (顺便说一句,我正在使用 Volley 集成)。
使用您自己的 ResourceDecoder。我仅加载本地文件并在 decode()
中处理重试计数。如果您使用其他型号,只需更改为相应的界面即可。
Glide 4.0.0-SNAPSHOT 示例。 自定义资源解码器:
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
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
<application>
...
<meta-data
android:name="com.fileloader.glide.CustomGlideModule"
android:value="GlideModule" />
</application>