0% found this document useful (0 votes)
26 views19 pages

Unit IV

Uploaded by

hmoosa.hh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views19 pages

Unit IV

Uploaded by

hmoosa.hh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

PERSISTENT STORAGE

Files
• Android supplies some basic file-management
tools to help you deal with the file system.
Many of these utilities are located within the
java.io.File package.
• Android does supply some specialized utilities
for file management that are available from
the application Context.
• deleteFile — Enables you to remove fi les
created by the current application
• fileList — Returns a string array that includes
all the files created by the current application

• These methods are particularly useful for


cleaning up temporary fi les left behind if your
application crashes or is killed unexpectedly.
Using Application-Specific Folders to Store
Files
• Many applications will create or download fi les
that are specific to the application. There are two
options for storing these application-specific files:
internally or externally.
• Android offers two corresponding methods via the
application Context, getDir and
getExternalFilesDir, both of which return a File
object that contains the path to the internal and
external application file storage directory,
respectively.
• All files stored in these directories or the
subfolders will be erased when your
application is uninstalled.
• Both of these methods accept a string
parameter that can be used to specify the
subdirectory into which you want to place
your files.
• Files stored in the application folders should
be specific to the parent application and are
typically not detected by the media-scanner,
and therefore won’t be added to the Media
Library automatically.
Creating Private Application Files
• Android offers the openFileInput and openFileOutput
methods to simplify reading and writing streams from and
to files stored in the application’s sandbox.

String FILE_NAME = “tempfile.tmp”;


// Create a new output file stream that’s private to this
application.
FileOutputStream fos = openFileOutput(FILE_NAME,
Context.MODE_PRIVATE);
// Create a new file input stream.
FileInputStream fis = openFileInput(FILE_NAME);
• These methods support only those fi les in the
current application folder; specifying path
separators will cause an exception to be
thrown.
• If the fi lename you specify when creating a
FileOutputStream does not exist, Android will
create it for you.
• The default behavior for existing fi les is to
overwrite them; to append an existing
file,specify the mode
aContext.MODE_APPEND.
• By default, files created using the
openFileOutput method are private to the
calling application — a different application
will be denied access.
• The standard way to share a file between
applications is to use a Content Provider.
Alternatively, you can specify either
Context.MODE_WORLD_READABLE or
Context.MODE_WORLD_WRITEABLE when
creating the output fi le, to make it available in
other applications,
as shown in the following snippet:
String OUTPUT_FILE = “publicCopy.txt”;
FileOutputStream fos =
openFileOutput(OUTPUT_FILE,
Context.MODE_WORLD_WRITEABLE);

• You can find the location of fi les stored in your


sandbox by calling getFilesDir. This method will
return the absolute path to the fi les created
using openFileOutput:
File file = getFilesDir();
Log.d(“OUTPUT_PATH_”, file.getAbsolutePath());
reading data from
files
• 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. Its syntax is given below −

• FileInputStream fin = openFileInput(file);


• After that, you can call read method to read
one character at a time from the file and then
you can print it. Its syntax is given below −

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();
listing contents of a directory
• Add the following code to manifest.xml
• <uses-permission android:name =
"android.permission.WRITE_EXTERNAL_STOR
AGE"/>
• <uses-permission android:name =
"android.permission.READ_EXTERNAL_STORA
GE"/>
Shared Preferences
• Shared Preferences — When storing UI state,
user preferences, or application settings, you
want a lightweight mechanism to store a
known set of values.
• Shared Preferences let you save groups of
name/value pairs of primitive data as named
preferences.
Creating shared preferences
• Using the SharedPreferences class, you can
create named maps of name/value pairs that
can be persisted across sessions and shared
among application components running
within the same application sandbox.
• To create or modify a Shared Preference, call
getSharedPreferences on the current Context,
passing in the name of the Shared Preference
to change.
SharedPreferences mySharedPreferences =
getSharedPreferences(MY_PREFS,Activity.MODE
_PRIVATE);

• Shared Preferences are stored within the


application’s sandbox, so they can be shared
between an application’s components but
aren’t available to other applications.
• To modify a Shared Preference, use the
SharedPreferences.Editor class. Get the Editor
object by calling edit on the Shared
Preferences object you want to change.
SharedPreferences.Editor editor =
mySharedPreferences.edit();
Saving and retrieving data using Shared
Preference
• To save edits, call apply or commit on the
Editor object to save the changes
asynchronously or synchronously,respectively.
// Commit the changes.
editor.apply();
RETRIEVING SHARED PREFERENCES
• Accessing Shared Preferences, like editing and
saving them, is done using the
getSharedPreferences method.
• Use the type-safe get<type> methods to
extract saved values. Each getter takes a key
and a default value (used when no value has
yet been saved for that key.)
// Retrieve the saved values.
boolean isTrue =
mySharedPreferences.getBoolean(“isTrue”, false);
float lastFloat =
mySharedPreferences.getFloat(“lastFloat”, 0f);
int wholeNumber =
mySharedPreferences.getInt(“wholeNumber”, 1);
long aNumber =
mySharedPreferences.getLong(“aNumber”, 0);
String stringPreference =
mySharedPreferences.getString(“textEntryValue”,
“”);

You might also like