Friday 3 April 2015

HB Blog 66: Android Persistent Application Storage.

Storing data has always been a important factor of every programming platforms. In Android specifically, there are several options for you to save persistent application data as follows,

Shared Preferences
    Store private primitive data in key-value pairs.
Internal Storage
    Store private data on the device memory.
External Storage
    Store public data on the shared external storage.
SQLite Databases
    Store structured data in a private database.
Network Connection
    Store data on the web with your own network server.

Shared Preferences is simply and editor which allows to store data in key-value pair. It is generally managed by getters and setters.You can use SharedPreferences to save any primitive data: booleans, floats, ints, longs, and strings. 
To write values:
    Call edit() to get a SharedPreferences.Editor.
    Add values with methods such as putBoolean() and putString().
    Commit the new values with commit()
To read values, use SharedPreferences methods such as getBoolean() and getString().

Internal storage allows to store data on device memory and it is used for private data.
To create and write a private file to the internal storage:
    Call openFileOutput() with the name of the file and the operating mode. This returns a FileOutputStream.
    Write to the file with write().
    Close the stream with close().
To read a file from internal storage:
    Call openFileInput() and pass it the name of the file to read. This returns a FileInputStream.
    Read bytes from the file with read().
    Then close the stream with close().

External storage is the storage place provided on sd-card. The data is public and can be accces by users directly.In order to read or write files on the external storage, your app must acquire the READ_EXTERNAL_STORAGE or WRITE_EXTERNAL_STORAGE system permissions.
Below is the code snippet, to save database file to Sd-Card,
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 public static void saveDatabaseToSdcard(Context context) {
        try {
            InputStream myInput = new FileInputStream("/data/data/com.example/databases/" + "MyDB.db");

            File file = new File(Environment.getExternalStorageDirectory().getPath() + "/" + "MyDB.db");
            if (!file.exists()) {
                try {
                    file.createNewFile();
                } catch (IOException e) {
                }
            }

            OutputStream myOutput = new FileOutputStream(Environment.getExternalStorageDirectory().getPath() + "/" + "MyDB.db");

            byte[] buffer = new byte[1024];
            int length;
            while ((length = myInput.read(buffer)) > 0) {
                myOutput.write(buffer, 0, length);
            }

            //Close the streams
            myOutput.flush();
            myOutput.close();
            myInput.close();
        } catch (Exception e) {
        }
    }

SQLite Databases allows to store structured data in a private database. SQLite is a software library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine. The recommended method to create a new SQLite database is to create a subclass of SQLiteOpenHelper and override the onCreate() method, in which you can execute a SQLite command to create tables in the database.

Network Connection allows to store data on the web with your own network server. To store and retrieve data web-based services can be written. To do network operations, use classes in the following packages,

    java.net.*
    android.net.*

No comments:

Post a Comment