-
Notifications
You must be signed in to change notification settings - Fork 6.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fallback for Url : to another Url #606
Comments
+1 |
How about
That's what #451 is there for, put a +1 on that. |
Thanks @TWiStErRob , I will give it a try , I really concern about the convenient way for 404 status |
Looks like a duplicate of #451 |
This is an untested alternative solution: Glide.with(context).load(new YouTubeVideo("watsQDZ3KNw")).into(imageView);
// TODO https://github.com/bumptech/glide/wiki/Configuration#creating-a-glidemodule
class YouTubeGlideModule implements GlideModule {
@Override public void applyOptions(Context context, GlideBuilder builder) {
}
@Override public void registerComponents(Context context, Glide glide) {
glide.register(YouTubeVideo.class, InputStream.class, new YTVModelLoader.Factory());
}
}
class YouTubeVideo {
String id;
public YouTubeVideo(String id) {
this.id = id;
}
public String getSDUrl() {
return String.format(Locale.ROOT, "http://img.youtube.com/vi/%s/sddefault.jpg", id);
}
public String getMDUrl() {
return String.format(Locale.ROOT, "http://img.youtube.com/vi/%s/mddefault.jpg", id);
}
}
class YTVModelLoader implements StreamModelLoader<YouTubeVideo> {
private final ModelLoader<String, InputStream> loader;
public YTVModelLoader(ModelLoader<String, InputStream> loader) {
this.loader = loader;
}
@Override public DataFetcher<InputStream> getResourceFetcher(final YouTubeVideo model, final int width,
final int height) {
return new DataFetcher<InputStream>() {
private DataFetcher<InputStream> fetcher;
@Override public String getId() {
return model.id; // assuming that if SDUrl failed once, it'll fail the next time
// so it's better to cache MDUrl in that case for this video, forever
}
@Override public InputStream loadData(Priority priority) throws Exception {
try {
fetcher = loader.getResourceFetcher(model.getSDUrl(), width, height);
return fetcher.loadData(priority);
} catch (Exception ex) {
if (fetcher != null) {
fetcher.cleanup();
}
fetcher = loader.getResourceFetcher(model.getMDUrl(), width, height);
return fetcher.loadData(priority);
}
}
@Override public void cleanup() {
if (fetcher != null) {
fetcher.cleanup();
}
}
@Override public void cancel() {
if (fetcher != null) {
fetcher.cancel();
}
}
};
}
static class Factory implements ModelLoaderFactory<YouTubeVideo, InputStream> {
@Override public com.bumptech.glide.load.model.ModelLoader<YouTubeVideo, InputStream> build(Context context,
GenericLoaderFactory factories) {
return new YTVModelLoader(factories.buildModelLoader(String.class, InputStream.class));
}
@Override public void teardown() {
}
}
} |
Looks Feasible to my Solution , will try it . Thanks @TWiStErRob |
1 Assuming that if SDUrl failed once, it'll fail the next time - it is not always true , there could be timeout or network error or connection error , |
I used the default |
Is there a way to check the cache when trying to load the fallback url? The example code works great but it always loads the fallback url over the network instead of checking for cache first. |
With this solution you can't check cache, because there's no Glide load going on. You can however download the fallback image(s) once to your app's directory and then return a fetcher that loads that File. Alternatively look around the other issues which deal with similar problems, or create a new one if you think your use case is different. |
I have tried to use that above code in my app with okhttp after converting it to kotlin, but it has a real high processor usage after loading an image (300% indefinitely on the emulator). How could that happen? |
#454
#456
#451
I wish I could get Exception listener for 404
like if particular url returns 404 then retry with fallback url instead of drawable .
Example Url1 - http://img.youtube.com/vi/watsQDZ3KNw/sddefault.jpg
if Url1 returns 404 then fallback
http://img.youtube.com/vi/watsQDZ3KNw/mddefault.jpg
I am using Youtube Image API , which does always have mddefault.jpg and higher version might not available .
The text was updated successfully, but these errors were encountered: