How to request again if image download failed using glide - android

I'm try to download multiple images from server using the Glide
here is code
for (String url : list) {
RequestOptions requestOptions = RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.ALL);
Glide.with(this)
.asBitmap()
.load(url).addListener(new RequestListener<Bitmap>() {
#Override
public boolean onLoadFailed(#Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {
return false;
}
#Override
public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
Log.e("ProgressCheck", "onResourceReady: " + progress);
return false;
}
})
.apply(requestOptions)
.submit();
}
Code run perfectly but when the downloading image failed (any reason wifi disconnected or server not responding.etc) how to send the same request again??
or is there the better way download multiple images using Glide

I suggest you to make a separate method of loading image via glide.
Here is the pseudo code
private void loadImage(String URL){
// Your Glide code
//Inside onLoadFailed call loadImage() again.
//For number of attempts you can maintain one int and increment that on every attempt.
}

If error or fallback strategies are not useful to you, then in 4.3.0 version you can starting a new request on failure:
Glide.with(fragment)
.load(url)
.error(
Glide.with(fragment)
.load(url))
.into(imageView);
Learn more at https://bumptech.github.io/glide/doc/options.html#starting-a-new-request-on-failure

Related

Get bitmap from url with other Api calls parallelly using glide and Rxjava in Android

I am making two API calls parallel in the following way.
Single.zip(API1.subscribeOn(Schedulers.io()),
API2.subscribeOn(Schedulers.io()), Bifunction())).subscribe();
I would want to load a bitmap from an URL and send back the bitmap. Something like this
Single.zip(API1.subscribeOn(Schedulers.io()),
API2.subscribeOn(Schedulers.io()), getImageBitmap(), Function3())).subscribe();
In the method getImageBitmap I am trying to return Single<Bitmap>. Something like this.
Single.create(new SingleOnSubscribe<Bitmap>() {
#Override
public void subscribe(final SingleEmitter<Bitmap> emitter) throws Exception {
Glide.with(context).asBitmap().load(url)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.error(R.drawable.ic_login_error).addListener(new RequestListener<Bitmap>() {
#Override
public boolean onLoadFailed(#Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {
emitter.onSuccess(null);
return false;
}
#Override
public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
emitter.onSuccess(resource);
return false;
}
});
}
}).subscribeOn(AndroidSchedulers.mainThread());
I am not getting any results out of this. How to get Bitmap from Glide in the following approach?
While loading the image from Glide, we should either call the .submit() method or .into(...) in order to successfully get the bitmap or load the image.

Failed to load resource without exception detail

I use Glide to load a URL of image to image view, like this:
RequestOptions requestOptions = new RequestOptions().placeholder(R.drawable.image_place_holder).error(R.drawable.image_place_holder);
Glide.with(imageView.getContext())
.load(url)
.listener(new RequestListener() {
#Override
public boolean onLoadFailed(#Nullable GlideException e, Object model, Target target, boolean isFirstResource) {
// Log the GlideException here (locally or with a remote logging framework):
Log.e(TAG, "Load failed", e);
e.printStackTrace();
// You can also log the individual causes:
for (Throwable t : e.getRootCauses()) {
Log.e(TAG, "Caused by", t);
}
// Or, to log all root causes locally, you can use the built in helper method:
e.logRootCauses(TAG);
return false; // Allow calling onLoadFailed on the Target.
}
#Override
public boolean onResourceReady(Object resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) {
return false;
}
})
.apply(requestOptions)
.into(imageView);
But Glide throw an exception in onLoadFailed() and show the place holder image instead:
W/Glide: Load failed for https://abc.xyz/image.png with size [736x388]
class com.bumptech.glide.load.engine.GlideException: Failed to load resource
TAG : Load failed
class com.bumptech.glide.load.engine.GlideException: Failed to load resource
You can see the exception has no causes, and can't print stack trace.
I try to use the Picasso library or change to another URL, it works!
But I can't understand what happen with the image link. Can you explain this problem here?

Glide for Android - Url request is not sent out, no exception

Glide Version: 4.7.1
Integration libraries: okhttp 4.7.1
I am trying to access and image (PNG) on a server but the request is not even sent. Can the URL formation crash the lib without an error message ? Or maybe it's sent but there is neither a failure response in the Request Listener nor a success.
How can i trace the problem ?
I have tried following the execution chain but i can only see the preparation of the request.
I monitor the network with Charles and i can't see the request sent out
I am using a basic GlideApp module call
GlideApp.with(mApplicationContext).load(url).dontAnimate().listener(new RequestListener<Drawable>() {
#Override
public boolean onLoadFailed(#Nullable final GlideException e, final Object model, final Target<Drawable> target, final boolean isFirstResource) {
// Nothing arrives here
return false;
}
#Override
public boolean onResourceReady(final Drawable resource, final Object model, final Target<Drawable> target, final DataSource dataSource, final boolean isFirstResource) {
// Nothing arrives here
return false;
}
}).into(imageView);
Thanks
OK i figured it out.
I was passing an newly created ImageView in code that i was using to store the returned image temporarily. This had no dimensions.
This worked fine with the Picasso library, but stopped working when i changed to Glide. So passing a real ImageView that is visible in a layout, works.
Picasso had another issue with PNG's which is why i switched.

Downloading images from Cloudinary in Android

I'm trying to figure out what the optimal workflow would be for an image transformation process in which a transformed image is again uploaded to another API.
According to Cloudinary (http://cloudinary.com/documentation/image_transformations#resizing_and_cropping_images), I can access uploaded images with the following kind of URL structure and also simultaneously transform them: http://res.cloudinary.com/demo/image/upload/w_200,h_100/sample.jpg.
Assuming that sample.jpg already exists in Cloudinary, the provided link will fetch it with a image resize transformation already applied.
Can I simply provide this link to Picasso and turn it into a Bitmap?
Picasso.with(this)
.load("http://res.cloudinary.com/demo/image/upload/w_200,h_100/sample.jpg")
.into(new Target() {
#Override
public void onBitmapLoaded (final Bitmap bitmap, Picasso.LoadedFrom from){
/* Save the bitmap or do something with it here */
UploadBitmap(bitmap);
}
});
Sorry, don't really know how to do it with Picasso, but with Glide you can do the following
Glide.with(this).load("path").asBitmap().listener(new RequestListener<String, Bitmap>() {
#Override
public boolean onException(Exception e, String model, Target<Bitmap> target, boolean isFirstResource) {
return false;
}
#Override
public boolean onResourceReady(Bitmap resource, String model, Target<Bitmap> target, boolean isFromMemoryCache, boolean isFirstResource) {
return false;
}
}).into(500/*output width*/,500/*output height*/);
And yes, buy specifying w and h in path you can manipulate image scaling

Android Glide: How to download and cache bitmaps?

I am using Glide to download and cache images on Android. Everything works well except the fact that I don't want to load the bitmaps directly into the ImageView, I don't want to have the fade animation, neither image placeholders.
All I want is to create a global method which will help me to download the image all over the application.
public class MyApp extends Application {
public static void downloadImage(String url, final OnImageLoadedCallback callback) {
// And how to implement the listener ?
RequestListener<String, Bitmap> requestListener = new RequestListener<String, Bitmap() {
#Override
public boolean onException(Exception exc, String string, Target<Bitmap> target, boolean isFirstResource) {
callback.onDone(null);
return false;
}
#Override
public boolean onResourceReady(Bitmap bitmap, String string, Target<Bitmap> target, boolean isFromMemoryCache, boolean isFirstResource) {
callback.onDone(bitmap);
return false;
}
};
Glide.with(context)
.load(url)
.asBitmap()
.dontAnimate()
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.listener(requestListener);
}
}
The problem is that I don't know how to implement the listener. RequestListener isn't called at all.
Loads in Glide don't start until you make a call to into. The RequestListener interface observes requests, but isn't typically meant for handling results. Instead of using RequestListener, consider having your callback implement the Target interface and pass it in using into.
Alternatively you could just extend SimpleTarget and pass it in to each request in the same way you're trying to use RequestListener:
Target target = Glide.with(context)
...
.into(new SimpleTarget<Bitmap>(width, height) {
#Override
public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
callback.onDone(resource);
}
#Override
public void onLoadFailed(Exception e, Drawable errorDrawable) {
callback.onDone(null);
}
});
// At some point later, if you want to cancel the load:
Glide.clear(target);
You will want to provide a width and a height so that Glide can downsample and transform images appropriately. You may also run in to issues with cancellation if you're displaying these Bitmaps in views, in which case I'd strongly recommend making the view available to your loading API and passing the view to into which will handle sizing and cancellation for you.
I use Glide 3.7.0 and download images this way:
it is important to note - it executes asyncroniously
Glide.with(this)
.load(url)
.downloadOnly(new SimpleTarget<File>() {
#Override
public void onResourceReady(File resource, GlideAnimation<? super File> glideAnimation) {
LOGGER.debug("Photo downloaded");
}
});
When I need to show cached image, I use the same url and DiskCacheStrategy.SOURCE:
Glide.with(this)
.load(url)
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.into(imageView);
It is now even simpler on Glide 4.9.0.
Glide.with(this)
.downloadOnly()
.load(url)

Categories

Resources