Saturday, August 20, 2011

Using SharedPreferences and Data Storage – Android Development

Well, there is a lot of help online for creating a folder in the Android file system. A little bit of googling and you should be good with it. The reason i have posted this blog entry is for those who want stuffs in one page. Here it is, with explanation.

Objective: To create a folder in the public SD Card directory of android.

Public directory in android terms refers to directory which is not deleted when application is uninstalled. 

There is a great detail of information in the links at the references section.

The code below can be altered to make it create a non-public directory, to create folder only at the time of installation of application and so on. The only way to create a directory only at the time of installation is to create a SharedPreferences boolean key for and set it true / false when the folder is created.

The reason, i have created the folder in the application context is because, the folder will be created at the application installation and will be available across all activities. Depending on your application design, you could choose to create the folder either in ApplicationContext or ActivityContext.

For implementing in ActivityContext, simply change the Application to Activity

Here is code

import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;


public class ErApplication extends Application { // Activity or Application
    private static ErApplication singleton;
    private static final String ER_ROOT_FOLDER = "er";
    private static final String ER_ROOT_FOLDER_PREFERENCES = "RootFolder";
    public static ErApplication getInstance()  {
        return singleton;
    /** Called when the application is first created. */
    public final void onCreate() {
        singleton = this;
        /** Root folder creation on first time installation of application */
        try {
        } catch (IOException e) {
            /** Unable to create file, because external storage is not mounted */
            Log.w("ExternalStorage", "Error creating directory", e);
    /** SharedPreferences configuration for setting folder creation flag */
    private void SetFolderCreationPreferences() throws IOException {
        /** Get the default SharedPreferences object */
        SharedPreferences mSharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
        /** Checks if key "RootFolder" exists in preferences else returns
         * the default value of true
        boolean mCreateRootFolder = mSharedPrefs.getBoolean(ER_ROOT_FOLDER_PREFERENCES, false);
        if(mCreateRootFolder) {
              /** Do some crap */
        } else {
            /** Set "NoRootFolder" key to true and commit the preference value*/
            SharedPreferences.Editor mSharedprefsEditor = mSharedPrefs.edit();
            mSharedprefsEditor.putBoolean(ER_ROOT_FOLDER_PREFERENCES, true);

    /** SharedPreferences configuration for setting folder creation flag */
    public static void CreatePublicFolder(String publicFolderName) throws IOException {
        /** You may want to have a check for MEDIA_MOUNTED here for safety
          * I did not add that, but its just 1 line “if” code
        File publicDirectory = new File(Environment.getExternalStorageDirectory() + "/" + publicFolderName);


[1] Environment -

[2] SharedPreferences -

[3] Data Storage -


No comments:

Post a Comment