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.
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.
See also:
Custom Rating Bar in Android
Then we make an Object of File to pass it in the OutputStream. After passing
the file object in outputstream then we will call the compress method of
Bitmap and passing some parameters. Let's see what parameters are they?
The first parameter is an image format that we want to save. Here we are using Bitmap.CompressFormat.JPEG for jpg images. The second parameter is image quality and we set it to 100 as we don't want to lose any quality of an image and the last parameter is our outputstream variable.
After all this work, when we successfully saved the image we need to close the outputstream by calling its close method. Then we notify the user for the image saved. That's how you can save an image using glide in android.
Hopefully, this helped you in some way.
Thanks for reading this article and if you like this article then do share it with your batch mates or friends. You can also subscribe to our newsletter for updates on android and other technology.
Share this Article 👇
Subscribe to our Newsletter
By subscribing to our newsletter you're agreeing with our T&C.