Recent Posts

Site Creator

Saving an Image or File in Device in Android

Posted on

Dear Developers

Few days back we faced this type of scenario where we had to save a image in device and later on post that image to Facebook .


why this is required

This is required since we want our application to save some data to persistent storage. so in case we close application we can get the data back. For eg , we have large set of bitmaps or images that first fetched from network and can be used by application.

Since these for fetching these bitmaps or images files from network ,  our application has utilised network a lot. and now if we need those images again in different functionality of app  we don’t want to fetch images from network again.

Instead we try to show images what already have. In technical terms we call that place a temp memory or cache.


For saving an image  in Android we use


We can save files in android in internal or external storage . For saving to external storage we need to handle runtime permissions , a model that was  introduced with Android Marshmallow.

To know how we use runtime permissions model while saving a file in Android , please visit my another blog post on runtime permissions model. Runtime permission

Although the blog post is based on Location permission but it is equally valid of a permission of Reading or writing external storage. [ you need to tweek code for doing this ].

So in the above code you are saving an image file  to the external storage using FileOutputStream.

Below is another variant of code where we are writing a text file to the external storage using output stream writer.





Thanks for studying.

Stay tuned to read more.
















Disclaimer: The aforesaid writeup by Relsell Global writer is for the general understanding of the readers. It does not render any professional advice or opinion.