3.2.2 Saving Data/Settings & File
Refer
to Android
lessons
for saving data. Common cases are:
-
Saving key-value pairs of simple data types in a shared preferences file
-
Saving arbitrary files in Android's file system
-
Using databases managed by SQLite =>refer to http://developer.android.com/training/basics/data-storage/databases.html
Applications
often include settings
that allow users to modify app features and behaviors. To provide
settings for app, use Android's
Preference
APIs to build an interface that's consistent with the user experience
in other Android apps (including the system settings).
This
SO
post talks about how to save JSON with SharedPreferences,
by doing this:
Saving
json to SharedPreferences:
PreferenceManager.getDefaultSharedPreferences(context).edit()
putString("theJson",jsonObject.toString()).apply();
Getting the stored json:
JsonObject jsonObject = PreferenceManager.
getDefaultSharedPreferences(this).getString("theJson","");
Here is from codeproject
which illustrate several way for managing data in Android, with
sample and source code.
The
preference file is xml file stored under
/data/data/package_name/shared_prefs.
It's readable with root.
Might
need to encrypt it.
For
saving to SD card, refer to this SO
and Environment:
don't be confused by
the word "external"
here. This directory can better be thought as media/shared
storage.
It is a filesystem that can hold a relatively large amount of data
and that is shared across all applications (does not enforce
permissions). Traditionally this is an SD card, but it may also be
implemented as built-in storage in a device that is distinct from the
protected internal storage and can be mounted as a filesystem on a
computer.
To
distinguish whether "Environment.getExternalStorageDirectory()"
actually returned physically internal
or external
storage, call Environment.isExternalStorageEmulated().
If it's emulated,
than it's internal.
On newer devices that have internal
storage and sdcard
slot Environment.getExternalStorageDirectory()
will always return the internal storage. While on older devices that
have only sdcard as a media storage option it will always return the
sdcard. Some
suggests to use following code for access internal and external SD
cards:
System.getenv("EXTERNAL_STORAGE") =>/storage/emulated/legacy
System.getenv("SECONDARY_STORAGE") =>/
storage/extSdCarcd
Note,
Environment.getExternalStorageDirectory
return ref of File. To get the path, append '
.getAbsolutePath()'
.
0 Comments:
Post a Comment