How to Save Image to storage using Glide in Android

Akshay Rana GuJJar
3 min readJun 19, 2020

Save Image using Glide

save Image using Glide
save Image using Glide

Hello World, Today we are going to see how we can save the image using the Glide library. Sometimes we are already using glide dependency in our android project to load images from link and we want to download the same image in the android device. In that case, we can utilize glide to save that bitmap to our android storage. Continue reading on this article to see how to download images using glide dependency.

Glide Download Image to File

Before we go deep in, I want you to read first how to load image from URL using glide. In that article, I have explained how you can use glide in your project.

Okay, now I am assuming that you know how to use glide to load image from URL.

I have made this layout to make it easy to understand all the things.

Save Image to storage using Glide
Save Image to storage using Glide

In the above layout, it is self-explanatory but let me explain here is an imageview with a button to download the image on click.

Let’s see the logic behind the click button. Check the below code.

In the above code, on the click of a button, we are calling the donwloadImage method which accepts link as a string.

Below is the code in the donwloadImage method.

First, we are verifying whether we have permission to write in internal storage or not.

Note: Remember to add <uses-permission android:name=”android.permission.WRITE_EXTERNAL_STORAGE”/> in your android manifest file.

The code of verifyPermissions method looks like this.

Basically, we are checking permission on runtime.

Now back to the downloadImage method.

Next, we are taking the folder path where we will save our image and store that value to dirPath variable and then convert it into File Object for later use.

After that, we are retrieving the file name from the user but you are free to choose whatever you want to be your file name.

Now comes the main part of this article, we load the image exactly how we load the image using glide but the twist is that as you can see in the into() method we are passing CustomTarget abstract class and implements its methods.

In the onResourceReady method, we are getting the image as Drawable and then we are converting it in Bitmap.

Now we have our image as Bitmap and we can perform the saving operation of image.

After getting the bitmap, we are calling the saveImage method by passing some parameters.

The first parameter is a bitmap, the second parameter is the directory path as File (which we previously made the variable) and the last and third parameter is file name.

Let’s see whats in the saveImage method.

First, we create a directory where we are going to save the image. for that first, we look if the directory already exists or not and if it does not exist then we will make a new directory.

On successfully directory creation we save that check as boolean in successDirCreated variable and then on the behalf of that perform the actions.

Hey if you want to read the full story go here Save Image to storage using Glide

Thanks for reading all the best.

--

--