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.app.Application;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;
import java.io.IOException;

import com.mvl.android.er.utils.ActivityUtils;

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. */
    @Override
    public final void onCreate() {
        super.onCreate();
        singleton = this;
       
        /** Root folder creation on first time installation of application */
        try {
            SetFolderCreationPreferences();
        } 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 {
            CreatePublicFolder(ER_ROOT_FOLDER);           
            /** Set "NoRootFolder" key to true and commit the preference value*/
            SharedPreferences.Editor mSharedprefsEditor = mSharedPrefs.edit();
            mSharedprefsEditor.putBoolean(ER_ROOT_FOLDER_PREFERENCES, true);
            mSharedprefsEditor.commit();
        }
    }

    /** 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);
        publicDirectory.mkdirs();
    }
}

References

[1] Environment - http://developer.android.com/reference/android/os/Environment.html

[2] SharedPreferences - http://developer.android.com/reference/android/content/SharedPreferences.html

[3] Data Storage - http://developer.android.com/guide/topics/data/data-storage.html

 

No comments:

Post a Comment