Saving an Image or File in Device in Android

1 min read

Android Development

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

File fileRoot = new File(Environment.getExternalStoragePublicDirector(),DIR_NAME); // assuming directo
is already there.

String fileName = fileRoot.getPath() + File.separator + "xx.jpg";
File file = new File(fileName);

if (file != null) {
    FileOutputStream fout;
    try {
        fout = new FileOutputStream(file);
        bm.compress(Bitmap.CompressFormat.JPEG, 90, fout);
        fout.flush();

        // now upload to facebook
        

    } catch (Exception e) {
        e.printStackTrace();
    }

}

 

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.

 

 

public void writeToFile(String data)
{
    // Get the directory for the user's public pictures directory.
    final File path = new File(Environment.getExternalStoragePublicDirectory,DIR_NAME); 

    // Make sure the path directory exists.
    if(!path.exists())
    {
        // Make it, if it doesn't exit
        path.mkdirs();
    }

    final File file = new File(path, "config.txt");

    // Save your stream, don't forget to flush() it before closing it.

    try
    {
        file.createNewFile();
        FileOutputStream fOut = new FileOutputStream(file);
        OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
        myOutWriter.append(data);

        myOutWriter.close();

        fOut.flush();
        fOut.close();
    }
    catch (IOException e)
    {
        Log.e("Exception", "File write failed: " + e.toString());
    } 
}

 

 

Thanks for studying.

Stay tuned to read more.