BCA TY Mobile Application Development UNIT IV Managing Data Storage
BCA TY Mobile Application Development UNIT IV Managing Data Storage
BCA TY Mobile Application Development UNIT IV Managing Data Storage
Android provides many ways of storing data of an application. One of this way is
called Shared Preferences. Shared Preferences allow you to save and retrieve
data in the form of key,value pair.
In order to use shared preferences, you have to call a method
getSharedPreferences() that returns a SharedPreference instance pointing to
the file that contains the values of preferences.
SharedPreferences sharedpreferences =
getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
MODE_APPEND
This will append the new preferences with the already existing preferences
MODE_PRIVATE
It is a default mode. MODE_PRIVATE means that when any preference file is
created with private mode then it will not be accessible outside of your
application. This is the most common mode which is used.
MODE_WORLD_READABLE
If developer creates a shared preference file using mode world readable then it
can be read by anyone who knows it’s name, so any other outside application
can easily read data of your app. This mode is very rarely used in App.
MODE_WORLD_WRITEABLE
It’s similar to mode world readable but with both kind of accesses i.e read and
write. This mode is never used in App by Developer.
You can save something in the sharedpreferences by using
SharedPreferences.Editor class. You will call the edit method of
SharedPreference instance and will receive it in an editor object.
Editor editor = sharedpreferences.edit();
editor.putString("key", "value");
editor.commit();
Apart from the putString method , there are methods available in the editor
class that allows manipulation of data inside shared preferences.
clear()
It will remove all values from the editor
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Name, n);
editor.putString(Phone, ph);
editor.putString(Email, e);
editor.commit();
Toast.makeText(MainActivity.this,"Thanks",Toast.LENGTH_LONG).show();
Prepared by: Mr. G.P.Shinde , COCSIT Latur Page 1
Notes: Mobile Application Development, Class: BCA TY, Unit IV: Managing Data Storage,
Writing file
In order to use internal storage to write some data in the file, call the
openFileOutput() method with the name of the file and the mode. The mode
could be private , public e.t.c.
Reading file
In order to read from the file you just created , call the openFileInput() method
with the name of the file. It returns an instance of FileInputStream.
After that, you can call read method to read one character at a time from the file
and then you can print it.
int c;
String temp="";
while( (c = fin.read()) != -1){
temp = temp + Character.toString((char)c);
}
//string temp contains all the data of the file.
fin.close();
Apart from the the methods of write and close, there are other methods
provided by the FileOutputStream class for better writing files.
External Storage
External Storage is useful to store the data files publically on the shared
external storage using the FileOutputStream object. After storing the data files
on external storage, we can read the data file from external storage media using
a FileInputStream object.
The data files saved in external storage are word-readable and can be modified
by the user when they enable USB mass storage to transfer files on a computer.
To read or write files on the external storage, our app must acquire the
WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE system
permissions. For that, we need to add the following permissions in the android
manifest file
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
we are creating and writing a file in device public Downloads folder by using
getExternalStoragePublicDirectory method. We used write() method to
write the data in file and used close() method to close the stream.
Read a File from External Storage
By using the android FileInputStream object and
getExternalStoragePublicDirectory method, we can easily read the file from
external storage.
SQLiteOpenHelper class
The android.database.sqlite.SQLiteOpenHelper class is used for database
creation and version management. For performing any database operation, you
have to provide the implementation of onCreate() and onUpgrade() methods
of SQLiteOpenHelper class.
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
onCreate(db);
}
Prepared by: Mr. G.P.Shinde , COCSIT Latur Page 4
Notes: Mobile Application Development, Class: BCA TY, Unit IV: Managing Data Storage,
if(deletedRows > 0)
Toast.makeText(MainActivity.this,"Data
Deleted",Toast.LENGTH_LONG).show();
else
Toast.makeText(MainActivity.this,"Data not
Deleted",Toast.LENGTH_LONG).show();
}
}
);
}
The End
Prepared by: Mr. G.P.Shinde , COCSIT Latur Page 6