Friday 9 October 2015

Storage Options

Storage Options

  • Android provides several options for you to save persistent application data.
  • The solution you choose depends on your specific needs, such as whether the data should be private to your application or accessible to other applications (and the user) and how much space your data requires. 


 Your data storage options are the following:
Store private primitive data in key-value pairs.
Store private data on the device memory.
Store public data on the shared external storage.
Store structured data in a private database.
Store data on the web with your own network server.
Using Shared Preferences:
  • The SharedPreferences class provides a general framework that allows you to save and retrieve persistent key-value pairs of primitive data types. 
  • You can use SharedPreferences to save any primitive data: booleans, floats, ints, longs, and strings. 
  • This data will persist across user sessions (even if your application is killed).
To get a SharedPreferences object for your application, use one of two methods:
  • getSharedPreferences() - Use this if you need multiple preferences files identified by name, which you specify with the first parameter.


  • getPreferences() - Use this if you need only one preferences file for your Activity. Because this will be the only preferences file for your Activity, you don't supply a name.
    To write values:
  • Add values with methods such as putBoolean() and putString().
  • Commit the new values with commit().

    Example : 
    To Store the data into Shared preferences
    SharedPreferences sharedpreferences = getSharedPreferences(“MyPreferences”, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedpreferences.edit();
    editor.putString(“Myname”, “xyz”);
    editor.commit();

    To get the data from Shared preferences


    String name = SharedPreferences.putString(“Myname”,””);


Using the Internal Storage:
  • You can save files directly on the device's internal storage. By default, files saved to the internal storage are private to your application and other applications cannot access them (nor can the user). 

  • When the user uninstalls your application, these files are removed.

    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:
  1. Call openFileInput() and pass it the name of the file to read. This returns a FileInputStream.
  2. Read bytes from the file with read().
  3. Then close the stream with close().
Saving cache files:
  • If you'd like to cache some data, rather than store it persistently, you should use getCacheDir() to open a File that represents the internal directory where your application should save temporary cache files.

  • When the device is low on internal storage space, Android may delete these cache files to recover space.

  • However, you should not rely on the system to clean up these files for you. You should always maintain the cache files yourself and stay within a reasonable limit of space consumed, such as 1MB. When the user uninstalls your application, these files are removed.
Using the External Storage:
  • Every Android-compatible device supports a shared "external storage" that you can use to save files. 

  • This can be a removable storage media (such as an SD card) or an internal (non-removable) storage.

  • Files saved to the external storage are world-readable and can be modified by the user when they enable USB mass storage to transfer files on a computer.
Getting access to external storage:
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.
For example:
<manifest ...>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    ...
</manifest>
If you need to both read and write files, then you need to request only the WRITE_EXTERNAL_STORAGE permission, because it implicitly requires read access as well.

Example : 
FileOutputStream outputStream;
File file = new  
File(Environment.getExternalStoragePublicDirectory(
           Environment.DIRECTORY_PICTURES), “hai.txt”);
try {
  outputStream = new FileOutputStream(file);
  outputStream.write(string.getBytes());
  outputStream.close();
} catch (Exception e) {
 e.printStackTrace();

}
Using Databases:
  • Android provides full support for SQLite databases. Any databases you create will be accessible by name to any class in the application, but not outside the application.

  • 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.

  • You can then get an instance of your SQLiteOpenHelper implementation using the constructor you've defined. 

  • To write to and read from the database, call getWritableDatabase() and getReadableDatabase(), respectively. These both return a SQLiteDatabase object that represents the database and provides methods for SQLite operations.
    • You can execute SQLite queries using the SQLiteDatabase query() methods, which accept various query parameters, such as the table to query, the projection, selection, columns, grouping, and others. 

    • For complex queries, such as those that require column aliases, you should use SQLiteQueryBuilder, which provides several convienent methods for building queries.

    • Every SQLite query will return a Cursor that points to all the rows found by the query.

    • The Cursor is always the mechanism with which you can navigate results from a database query and read rows and columns.
    Using a Network Connection:
    You can use the network (when it's available) to store and retrieve data on your own web-based services. To do network operations, use classes in the following packages:



   

0 comments:

Post a Comment