Sun. Jan 20th, 2019

Loading images from Android Glide Library

Loading images from Android Glide Library | Glide is a popular Android library for image downloading and caching.It is a fast and efficient open source media management and image loading framework for Android.Glide’s primary focus is on making scrolling any kind of a list of images as smooth and fast as possible.It is also used for fetch, resize, and display a remote image.In this example, we will learn how to use Android Glide Library.

OutPut-

Download Project – GlideDemo

Download Apk – Apk

Wants to Learn Advanced Android Application development from scratch- Beyond Basics

Creating New Project – GlideDemo

Open your Android Studio & create a new Project, have taken Blank Activity for this project and clicked finish.

Adding Android Glide Library

In-app level, build.gradle file inside dependencies block add the following dependencies

compile 'com.github.bumptech.glide:glide:3.7.0'

then sync the project.

Creating Layout

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_height="match_parent">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />
</RelativeLayout

We are going to load pictures into ImageView using Android Glide Library.So this XML file contains one imageview.

Related:

Upload image from gallery to firebase in Android

Circular Imageview in Custom Listview in Android

Printing Images from your Android Application, Using Kotlin

Image Compression in Android

Image blur using Renderscript in Android Application

MainActivity.Java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
public class MainActivity extends AppCompatActivity {
    ImageView imageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView = (ImageView)findViewById(R.id.imageView);
        Glide.with(this)
                .load(R.drawable.image)
                .into(imageView);
    }
}

We can use glide library as different operations like –

  • Loading image from res/drawable folder
Glide.with(this)
    .load(R.drawable.image)
    .into(imageView);
  • Loading image present in server
Glide.with(this)
    .load("image_url")
    .into(imageView);
  • Loading image from any file location
Glide.with(this)
    .load(new File("file_location"))
    .into(imageView);
  • Show placeholder image

Show placeholder image while original image is being loaded.

Glide.with(this)
       .load("image_url")
       .placeholder(R.drawable.image_name)
       .into(imageView);
  • Show error image

When error occurs while loading original image, you have to write below code

Glide.with(this)
       .load("image_url")
       .error(R.drawable.image_name)
       .into(imageView);
  • Transform image

If you want to resize it, crop it. you can do

Glide.with(this)
       .load("image_url")
       .override(200, 200)
       .centerCrop()      
       .into(imageView);

 

Conclusion
In this Project, you saw how to use Android Glide Library.I hope you will understand the method. In the case of any queries, you may ask questions. Keep following more amazing Android Blogs. If You are Beginner Learn Android application development from the scratch


If you really liked the article, please subscribe to our YouTube Channel for videos related to this article.Please find us on Twitter and Facebook.

Leave a Reply

Your email address will not be published. Required fields are marked *