Learning how to Save, Load, and Delete Bitmaps in Android Studio — Using Java
I recently came across a situation where I wanted to show some images in my app. Finding the solution was difficult as there are a lot of opinions online for best practice. As we progress things get outdated and deprecated. So what, right now as of 29/04/2021 works for me is the following:
Saving Bitmaps
Ensure that each file you want to save has a unique name. I have a function in my ImageProcessing.java that appends a string date and time value to ensure this.
Let’s actually save the file now and discuss what is happening in the code. Our aim is to write a function that takes in a bitmap and a friendly name so we can use the method above to create a unique name within our save function. We then want to use an output file stream to write our bitmap to a file. We're then going to compress it to our desired format and quality. Finally, we’ll close the stream to prevent any memory leaks and return the file path so we can save this path in our database.
Pro Tip: You may use the
bitmapImage.compress()
function to compress an image even further to say50
percent of the original quality. That would have a slight performance advantage if the user is scrolling any list view that is heavy on images. I’ve defaulted this to100
for the demonstration.
Retrieving Bitmaps
You did it, you managed to save the bitmap to storage and the returned file path to your database. How then do we retrieve the image we have just saved? Well let's first consider your context:
- Do you want this image to be at full quality?
- Are you planning on putting this image in a listview item or on a detail page?
- Does the size matter?
Let me tell you. How you retrieve the image matters for the sake of performance and memory so it’s important that we consider this. Firstly then I am going to create an enum to specify which type of image I want. This will give context for my bitmap options such as scale and quality. This, in turn, will keep all of my bitmaps for lists and detail pages the same size and quality offering consistency to the user across my whole app.
Now we’re gonna write a method to return our bitmap options based upon this enum. This way we know we can reduce the quality. Here I have reduced the size of the ListView
image by 3/4 and saying I want 1/16 of the original number of pixels, however, I want to load a normal size full quality image in the detail view. There are loads of options here we can use. Here’s the part about inSampleSize
as this is what I am using below. https://developer.android.com/reference/android/graphics/BitmapFactory.Options#inSampleSize
Now let’s put this together in a retrieve function. I want to set my bitmap options and finally return my bitmap.
Pro Tip: Always return a default image, in a list or on a detail page and where ever really haha! This looks way better than an empty ImageView.
Now we have successfully returned the right image for the right context. Good Job 👍
Delete Bitmaps
Now let us not clog our user's phone. Let’s remove all files and images associated with a user when they delete our app or when they remove an item that correlates to a saved image. I’ve seen so many examples of this and it’s concerning how big our apps can get using the users’ phone storage for our images. Please remove them if the context applies.
What we’re gonna do a little function now to delete the image taking in a file path to the image, creating a file from that, and then using the .delete()
function to delete it.
And there you have it. You’ve saved, retrieved, and deleted a file from Android storage. Nice 👍
Any questions? Think you have a better way of doing this? Shoot me a message. I’ll love to hear about it.