

Android Save Bitmap to Gallery – Download and Save Image from URL
source link: https://www.simplifiedcoding.net/android-save-bitmap-to-gallery/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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 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.

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.

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.
Recommend
-
41
-
14
Library Android choosing Image from Camera / Gallery with Crop Functionality By
-
11
Android Bitmap小技巧大家都知道,相机在照相时是会为照片生成Exif,里面包含有拍照时间、图片尺寸、旋转角度、GPS定位等信息,有时候,我们的APP在展示图片的时候需要获得并展现这些信息。
-
11
Android异步加载全解析之Bitmap 在这篇文章中,我们分析了Android在对大图处理时的一些策略——Android异步加载全解析之大图处理
-
9
Android Bitmap缓存池使用详解 – Android开发中文站你的位置:Android开发中文站 > Android开发 >
-
9
笔者最近致力于vivo游戏中心稳定性维护,在分析线上异常时,发现有相当一部分是由OutOfMemory引起。谈及OOM,我们一般都会想到内存泄漏,其实,往往还有另外一个因素——图片,如果对图片使用不当的话,很容易吃掉大量内存,从而导致异常。 尤其是游戏中...
-
13
How do I programmatically rotate a bitmap image stored as a 1D table? advertisements i have a class, Bitmap,which simply contains a pixel arra...
-
7
Android对Bitmap的内存优化方案总结 – Android开发中文站你的位置:Android开发中文站 > Android开发 >
-
7
1、BitmapFactory解析Bitmap的原理 BitmapFactory提供的解析Bitmap的静态工厂方法有以下五种: Bitmap decodeFile(...)Bitmap decodeResource(...)B...
-
7
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK