Show Gif File with Glide (Image Loading and Caching Library)

How to show gif on Glide

From This and this questions.

build.gradle(app-level)

dependencies {
implementation 'com.github.bumptech.glide:glide:4.8.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'
}

Use of Glide.

Glide.with(context)
.load(imgUrl)
.asGif()
.placeholder(R.drawable.img)
.crossFade()
.into(imageView);

Glide: How to resize and save the gif as file using Glide v4?

Glide can not only load files, but also download them. And that is what you want. You can just use this code and it will be downloaded.

Glide.with(MainActivity.this).asFile()
.load(url)
.apply(new RequestOptions()
.format(DecodeFormat.PREFER_ARGB_8888)
.override(Target.SIZE_ORIGINAL)) // you can also give your size here
.into(new Target<File>() {
@Override
public void onStart() {

}

@Override
public void onStop() {

}

@Override
public void onDestroy() {

}

@Override
public void onLoadStarted(@Nullable Drawable placeholder) {

}

@Override
public void onLoadFailed(@Nullable Drawable errorDrawable) {

}

@Override
public void onResourceReady(@NonNull File resource, @Nullable Transition<? super File> transition) {
storeImage(resource);
}

@Override
public void onLoadCleared(@Nullable Drawable placeholder) {

}

@Override
public void getSize(@NonNull SizeReadyCallback cb) {

}

@Override
public void removeCallback(@NonNull SizeReadyCallback cb) {

}

@Override
public void setRequest(@Nullable Request request) {

}

@Nullable
@Override
public Request getRequest() {
return null;
}
});

The storeImage method:

private void storeImage(File image) {
File pictureFile = getOutputMediaFile();
if (pictureFile == null) {
return;
}
try {
FileOutputStream output = new FileOutputStream(pictureFile);
FileInputStream input = new FileInputStream(image);

FileChannel inputChannel = input.getChannel();
FileChannel outputChannel = output.getChannel();

inputChannel.transferTo(0, inputChannel.size(), outputChannel);
output.close();
input.close();
Toast.makeText(MainActivity.this, "Image Downloaded", Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

private File getOutputMediaFile() {
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/Diwali Images"); // change the folder name according to your needs.
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs())
return null;
}

File mediaFile;
mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_SHUBH_DIWALI_"+Calendar.getInstance().getTimeInMillis() +".gif"); // change the name of the file according to your wish
return mediaFile;
}

Edit


I have actually found out 1 library for that. In that library, I found this class interesting. Here, in the constructor we can pass the width and height and then we can save it.

Is it possible to show GIF and also JPG image on the same adapter using Glide?

You can do like this

String str = "image or gif url";
if (str.contains(".gif")){
Glide.with(this)
.asGif()
.load(str)
.into(imgeview);
}else{
Glide
.with(this)
.load(str)
.centerCrop()
.into(myImageView);
}

not able to display animated gif in imageview in android using glide

It works but may take time in loading gif.

ImageView imageView = (ImageView) findViewById(R.id.imageView);
GlideDrawableImageViewTarget imageViewPreview = new GlideDrawableImageViewTarget(imageView);
Glide.with(this)
.load("http://i.kinja-img.com/gawker-media/image/upload/s--B7tUiM5l--/gf2r69yorbdesguga10i.gif")
.listener(new RequestListener<String, GlideDrawable>() {
@Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
Log.e("MyApp", "onException: "+model+" Exception: "+e );
return false;
}

@Override
public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
Log.e("MyApp", "onResourceReady: "+model );
return false;
}
})
.into(imageViewPreview);


Related Topics



Leave a reply



Submit