Android Save Bitmap to Gallery – Download and Save Image from URL

Things are changing very fast in tech world. And if you want to be an Android Developer, then be ready to keep upgrading yourself. Actually this learning curve is the best thing that I like …

android save bitmap to gallery app copy

Things are changing very fast in tech world. And if you want to be an Android Developer, then be ready to keep upgrading yourself. Actually this learning curve is the best thing that I like about this industry (My personal opinion). So, here is a new about about “Android Save Bitmap to Gallery”.

Saving file to internal storage, is required many times in our projects. We have done this thing many times before, but the method for saving file to internal storage has been changed completely for devices running with OS >= android10.

Basically your old way of saving files will not work with new versions of android, starting with android10. You can still go the old way, but it is a temporary solution. In this post we are going to talk about all these things.

File Storage in Android

Now, we have two kinds of storage to store files.

  • App-specific storage: The files for your application only. Files store here cannot be accessed outside your application. And you do not need any permission to access this storage.
  • Shared Storage: The files that can be shared with other apps as well. For example Media Files (Images, Videos, Audios), Documents and other files.

To know more details about File Storage in Android, you can go through this official guide.

In this post we will be saving a Bitmap to Shared Storage as an Image File.

To demonstrate saving file to internal storage in android10, I will be creating an app that will fetch an Image from the Given URL and then it will save it to external storage.

Android Save Bitmap to Gallery

Let’s first start with the main function, that we need to save a Bitmap instance to gallery as an Image File.

Let’s understand the above function.

  • We have the bitmap instance that we want to save to our gallery in the function parameter.
  • Then I’ve generated a file name using System.currentTimeMillis()  , you apply your own logic here if you want a different file name. I used it just to generate a unique name quickly.
  • Now we have created an OutputStream  instance.
  • The main thing here is we need to write two logics, because method of android 10 and above won’t work for lower versions; and that is why I have written a check here to identify if the device is >= VERSION_CODES.Q .
  • Inside the if block, first I’ve got the ContentResolver  from the Context .
  • Inside ContentResolver , we have created ContentValues and inside ContentValues  we have added the Image File informations.
  • Now we have got the Uri  after inserting the content values using the insert()  function.
  • After getting the Uri , we have opened output stream using openOutputStream()  function.
  • I am not explaining the else part, as it is the old way, that we already know.
  • Finally using the output stream we are writing the Bitmap to stream using compress()  function. And that’s it.

Now let’s build a complete application that will save image from an URL to the external storage. 

Creating an Image Downloader Application

Again we will start by creating a new Android Studio project. I have created a project named ImageDownloader. And now we will start by adding all the required dependencies first.

Adding Permissions

For this app we require Internet and Storage permission. So define these permision in your AndroidManifest.xml  file.

Also make sure you add android:usesCleartextTraffic="true"  as I did.

Setting Up Dependencies

We will add the following dependencies inside app level build.gradle  file.

Designing UI

I have designed the following UI for the application.

Android Save Bitmap to Gallery
Android Save Bitmap to Gallery

The UI is pretty simple and I hope you can make a UI like this easily. I am not posting the XML code here; but if you need it you can get the source code at the end of this post.

So basically here we have an EditText to input an Image URL. Two buttons, one to paste the copied URL and another one to Download the image from the given URL.

Creating Utils Function

Create a file named Utils.kt  and write the following code.

We will be using these functions, in our MainActivity .

Downloading and Saving Bitmap to Gallery

Now we just need to add the functionality in our MainActivity.kt  file. The code is very straight forward so I am directly showing the whole MainActivity  here.

The code is very straight forward, but in case you have a problem; don’t hesitate to ask.

Now you can run your application and you will see the following result.

android save bitmap to gallery app
Image Downloader
I have created a special space for
Simplified Coding in Quora.
You can join here to ask your question in detail.

Android Save Bitmap to Gallery Source Code

In case you need the source code of this tutorial, you can get it from here.

So that is all for this tutorial friends. Make sure you share it with your friends, if you found this post helpful. Thank You

Hi, my name is Belal Khan and I am a Google Developers Expert (GDE) for Android. The passion of teaching made me create this blog. If you are an Android Developer, or you are learning about Android Development, then I can help you a lot with Simplified Coding.

Expand Your Knowledge: Next Tutorial Picks

0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x