Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@JoachimR
Last active September 15, 2021 01:54
Show Gist options
  • Star 21 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save JoachimR/c7fd2408d11a9fe67ee9 to your computer and use it in GitHub Desktop.
Save JoachimR/c7fd2408d11a9fe67ee9 to your computer and use it in GitHub Desktop.
import android.content.Context;
import android.support.annotation.NonNull;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.data.DataFetcher;
import com.bumptech.glide.load.model.stream.StreamModelLoader;
import java.io.IOException;
import java.io.InputStream;
public class GlideHelper {
/**
* Fill an ImageView with a picture from the resources using Glide.
*
* @param context the Context for where to load
* @param imageView the ImageView to fill with an image
* @param resDrawableId the resource drawable id
*/
public static void resDrawableToImageView(Context context, ImageView imageView, int resDrawableId) {
if (context == null || imageView == null) {
return;
}
Glide.with(context)
.load(resDrawableId)
.thumbnail(0.1f)
.into(imageView);
}
/**
* Fill an ImageView with a picture from an http link using Glide.
*
* @param context the Context for where to load
* @param imageView the ImageView to fill with an image
* @param imageUrl the image url from which Glide should download and cache the image
* @param placeholderDrawableResId the resource id of the image that should be used as a placeholder image
*/
public static void urlToImageView(Context context, ImageView imageView, @NonNull String imageUrl,
int placeholderDrawableResId) {
urlToImageView(context, imageView, imageUrl, placeholderDrawableResId, false);
}
/**
* Fill an ImageView with a picture from an Http link using Glide.
*
* @param context the Context for where to load
* @param imageView the ImageView to fill with an image
* @param imageUrl the image url from which Glide should download and cache the image
* @param placeholderDrawableResId the resource id of the image that should be used as a placeholder image
* @param useCacheOnly whether to only use the cache to load the pictures or allow downloading the
* picture if the picture is not found in the cache.
*/
public static void urlToImageView(Context context, ImageView imageView, @NonNull String imageUrl,
int placeholderDrawableResId, boolean useCacheOnly) {
if (context == null || imageView == null) {
return;
}
if (useCacheOnly) {
Glide.with(context)
.using(cacheOnlyStreamLoader)
.load(imageUrl)
.placeholder(placeholderDrawableResId)
.thumbnail(0.1f)
.into(imageView);
return;
}
Glide.with(context)
.load(imageUrl)
.placeholder(placeholderDrawableResId)
.thumbnail(0.1f)
.into(imageView);
}
private static final StreamModelLoader<String> cacheOnlyStreamLoader = new StreamModelLoader<String>() {
@Override
public DataFetcher<InputStream> getResourceFetcher(final String model, int i, int i1) {
return new DataFetcher<InputStream>() {
@Override
public InputStream loadData(Priority priority) throws Exception {
throw new IOException();
}
@Override
public void cleanup() {
}
@Override
public String getId() {
return model;
}
@Override
public void cancel() {
}
};
}
};
}
@khoantt91
Copy link

Thanks so much. Help me a lot 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment