Monday 5 November 2018

Glide - Image Download library for android

What is Glide?

  • Glide is an open source media management framework for Android.
  • Control 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 
  • Glide’s primary focus is on making scrolling any kind of a list of images as smooth and fast as possible, but Glide is also effective for almost any case where you need to fetch, resize, and display a remote image.

Glide Unique Features

  1. Animated GIF decoding : you can load GIF animation into ImageView.
  2. Supports Thumbnail
  3. Supports OkHttp and Volley library & retrofilt library
  4. Placeholder can be added before loading the original image.
  5. Any custom image can be shown if error occurs while loading the original image.

Alternatives of Glide 

  •  Picasso  

Why Image loader library? 

  • To show an image into ImageView from a url we need to download that image
  • downloading the complete image from remote location, handle network errors if occurs while downloading. 
  • Implement image caching.
To download an image one should do a lot of things.
This will increase the lines of code and of course development time.
So its better to use the library to reduce our work and focus more on the development idea.

Steps to use Glide?

step 1.

 Add dependency for Glide into app/build.gradle.
dependencies {

implementation 'com.github.bumptech.glide:glide:4.8.0
} 

Step 2.

 Add INTERNET permission in manifest file 

 <uses-permission android:name="android.permission.INTERNET" /> 

Step 3

 Add ImageView in you layout

Declare an ImageView in your layout to display image from remote server in it
 <ImageView  
   android:layout_width="wrap_content"  
   android:layout_height="wrap_content"  
   android:id="@+id/imageView">  
 </ImageView>

Step 4 

 Add ImageView in you layout


 Glide.with(this)  
     .load("IMAGE URL HERE")  
     .placeholder(R.drawable.placeholder)  
     .error(R.drawable.imagenotfound)  
     .into(imageView);



MainActivity.java

 public class MainActivity extends AppCompatActivity {  
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_main);  
     final String url="https://avatars0.githubusercontent.com/u/1?v=4";  
     //Glide  
     Button btn=findViewById(R.id.btn);  
     final ImageView imageView=findViewById(R.id.img);  
     btn.setOnClickListener(new View.OnClickListener() {  
       @Override  
       public void onClick(View v) {  
         Glide.with(MainActivity.this).load(url).into(imageView);  
       }  
     });  
   }  
 }  

activity_main.xml

 <?xml version="1.0" encoding="utf-8"?>  
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   xmlns:app="http://schemas.android.com/apk/res-auto"  
   xmlns:tools="http://schemas.android.com/tools"  
   android:layout_width="match_parent"  
   android:orientation="vertical"  
   android:layout_height="match_parent"  
   tools:context=".MainActivity">  
   <ImageView  
     android:layout_width="wrap_content"  
     android:id="@+id/img"  
     android:src="@mipmap/ic_launcher"  
     android:layout_height="wrap_content" />  
   <Button  
     android:layout_width="wrap_content"  
     android:text="Download image and show"  
     android:id="@+id/btn"  
     android:layout_height="wrap_content" />  
 </LinearLayout>  

1 comment:

How to implement Facebook audience network banner ad in android app using Android Studio

Its very simple to implement Facebook audience network banner ad in android app using Android Studio. We will complete it in 4 Steps. 1. ...