You can also use Glide as it is officially supported by google for loading images,gifs etc with efficient memory management. I would suggest you to use the same, As I used it before and it worked perfectly for me.
Check it here https://github.com/bumptech/glide
Adding it to Gradle:
repositories {
mavenCentral()
google()
}
dependencies {
implementation 'com.github.bumptech.glide:glide:4.7.1'
annotationProcessor 'com.github.bumptech.glide:compiler:4.7.1'
}
How to use it for loading Gifs:
Glide.with(this).load(R.raw.file.gif)
.crossFade()
.dontAnimate()
.into(new GlideDrawableImageViewTarget(imageView) {
@Override
public void onLoadStarted(Drawable placeholder) {
super.onLoadStarted(placeholder);
//DO your loader related stuff here
}
@Override
public void onResourceReady(GlideDrawable drawable, GlideAnimation anim) {
super.onResourceReady(drawable, anim);
//Close loader here
imageView.setImageDrawable(drawable);
}
@Override
public void onLoadFailed(Exception e, Drawable errorDrawable) {
super.onLoadFailed(e, errorDrawable);
//Close loader and set error placeholder here.
imageView.setImageResource(R.drawable.ic_home_image_placeholder);
}
});
Also for checking that your file is gif or not you can use method shared below:
private int getResourceType(String assetName) {
int id = 0;
if (assetName != null && !assetName.trim().isEmpty()) {
int start = assetName.indexOf(".");
int end = assetName.length();
if(start >= 0 && end <= assetName.length()) {
String type = assetName.substring(assetName.indexOf("."), assetName.length());
if (type != null && type.equalsIgnoreCase(".gif")) {
id = 1;
} else if(type != null && (type.equalsIgnoreCase(".png") || type.equalsIgnoreCase(".jpg")
|| type.equalsIgnoreCase(".jpeg"))) {
id = 2;
} else {
id = 0;
}
}
}
return id;
}