Saving Data in Android Apps- Part 2

In the second part of this tutorial you will learn how to store basic files and perform basic file-related tasks in your app. Here’s the link to the other parts of the tutorial for your reference.

Saving Data In Android Apps- Part 1
Saving Data in Android Apps- Part 3

Saving Files

To save and interact with files you need to create a File object. A File object is suited to reading or writing large amounts of data in start-to-finish order without skipping around. For example, it’s good for image files or anything exchanged over a network.

External & Internal Storage

Every Android device by default has two file storages, internal and external in some cases even when the external microSD slots are not there the device storage is still divided in the internal and external partitions. The external partition is removable or not, the procedure and methods remain same. Files stored in the internal storage can be read only by your app. The user and other apps cannot access such files. The files stored in external storage can be accessed by any other app and also by the user. To use the external storage you need to obtain the appropriate permission.

Requesting Permission For External Storage

To get the permission to read and write to the external storage in your Android manifest xml file write the following code :

    <uses-permission android:name =“android.permission.WRITE_EXTERNAL_STORAGE” / >
    <uses-permission android:name =“android.permission.READ_EXTERNAL_STORAGE” />

You don’t need any permissions to save files on the internal storage. Your application always has permission to read and write files in its internal storage directory.

Saving a file on internal storage

Internal storage has two directories : the default app directory, and the cache directory of the app.
Save files that are required for long term in the default directory, And use the cache directory for temporary files required by your app. Just remember to delete the temporary files or the system might delete these files when the system runs low on memory.

Saving in Default directory

To create a new file in the default internal directory you can call openFileOutput() method to get a FileOutputStream that writes to a file in your internal directory. The following code shows how to do this:

String toWrite = “The String that you are gonna save in the file”;
FileOutputStream outputS;
    // openFileOutput(String filename,int mode)
    // mode:-  Operating mode. Use 0 or MODE_PRIVATE for the default operation, 
    //MODE_APPEND to append to an existing file, MODE_WORLD_READABLE and 
    //MODE_WORLD_WRITEABLE to control permissions.
    outputS = openFileOutput(“textfile”,Context.MODE_PRIVATE);
    //write() method writes to the file
    //close() method is required to save the changes and close the file
}catch(Exception e){

Saving in Cache directory

To create a temporary file in the caches directory use createTempFile() method.
The following code shows how to create a temporary file in the cache directory:

// create a File object.
File file;
// assign a filename
String Filename = “mytempfile”;
    // createTempFile(String filename,String extension,File directory)
    // by default extension is .tmp if null is used
    // getCacheDir() gets the internal cache directory
    file = File.createTempFile(fileName, null, getCacheDir());
}catch(IOException e){
    //Error while creating file;
//now use any of the File methods to write to file 

Saving a File on External Storage

The following code shows how to write to External Storage:

//Check if external storage is available for read and write
String state = Environment.getExternalStorageState();
    // Available for read and write
    // Not available for read and write

To check if external storage is readable atleast :

//Check if external storage is available to at least read 
String state = Environment.getExternalStorageState();
if(Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)){
    //Available for reading
    //Not available

You can save two different types of file on external storage: Public files and Private files.
Both type of files is modifiable and readable by the user and other apps. The only difference is that private files are stored in the app directory and gets deleted when the app gets uninstalled, while the files stored as public files stay.

A sample code to save a public file:

//Getting the user’s public pictures directory.
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), “myFolder”);
//similarly you can use Environment.DIRECTORY_MUSIC to get public music directory.
    //Directory was not created.
// if the file gets created we can use the normal file methods to read and write.

A sample code to save a private file:

//Getting the app’s private pictures directory
File file = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), “myFolder”);
// we can get the root app directory we can pass null to getExternalFilesDir() method
    //Directory was not created
// if the file gets created we can use the normal file methods to read and write.

Delete a File

Delete a file that is no longer needed. The following simple sample code just that:

//myFile is an object of type File that references to a file residing on the system.

In this tutorial you learnt how to read and write simple files. In the next tutorial how to use SQL databases. Here’s a link to the next part of the tutorial.

Saving Data in Android Apps- Part 3

This content has been helpful to you?

Thanks for contributing!

Yes No