Loading Image from Server,Local file system,Drawable in Android
Loading Imagefile to imageview using Bitmap
This is the simple way to load file into the imageview without using any Lib.
To load file into imageview you need file path
File imgFile = new File("/sdcard/Images/test_image.jpg");if(imgFile.exists()){
Bitmap myBitmap=BitmapFactory.decodeFile(imgFile.getAbsolutePath());myImageView.setImageBitmap(myBitmap);
}
Loading With Glide
Glide is a fast and efficient open source media management and image loading framework for Android that wraps media decoding, memory and disk caching, and resource pooling into a simple and easy to use interface.
Glide supports fetching, decoding, and displaying video stills, images, and animated GIFs. Glide includes a flexible API that allows developers to plug in to almost any network stack.
To Add glide mage below module gradle changes
repositories {
google()
mavenCentral()
}
Add Dependency of Glide
dependencies {
implementation 'com.github.bumptech.glide:glide:4.12.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'
}
To Load Image from url use below code
To load image from server you need to pass the image url like i mentioned below,it requred context,place holder which will be shown untill image is not loaded,Imageview in which that url image is need to load.
Glide
.with(context) //Pass here context
.load(url) //Pass here imageurl to load
.placeholder(R.drawable.loading_spinner) //pass here placeholder which will be shown untill it loaded
.into(myImageView); //pass here your imageview
To Load Image from Local Drawable or from project.
To load image from Drawble folder just call the getImage() and pass the name of drawable it will retun int id of that drawable and then you can pass that int id to the glide to load an image.
var imageId=getImage(my_drawable_image_name)Glide.with(this).load(imageId).into(myImageView)//Use this method to get imageid
public int getImage(String imageName) {
int drawableResourceId = this.getResources().getIdentifier(imageName, "drawable", this.getPackageName());
return drawableResourceId;
}
To Load Image From File System of device
To load Image from file you need to pass file path in load method of builder function
Glide.with(context)
.load(filePath) //Pass here your local file Path
.into(myImageView);
Here is some Bonus tips for glide.
You can do many operation to modify the image or you can load image with animation.
Below are some way to do that.
Adding Animation while loading using TransitionOptions
TransitionOptions determine what will happen when your requested load completes.
Use TransitionOptions
to apply:
- View fade in
- Cross fade from the placeholder
- No transition
Code to use TransitionOptoins
Glide.with(fragment)
.load(url)
.transition(withCrossFade())//To Load image with animation
.into(view);
Transformations:
Glide includes a number of built in transformations, including:
You can also create custom Transformations you can find that implementation here
You can use below code for build in transformations
RequestOptions options = new RequestOptions();
options.centerCrop();
Glide.with(fragment)
.load(url)
.apply(options)
.into(imageView);
Happy Coding…;)