You can get the base directory name like this:
private File getDataDir(String packageName) {
try {
PackageInfo packageInfo =
getPackageManager().getPackageInfo(packageName, 0);
if (packageInfo == null) return null;
ApplicationInfo applicationInfo =
packageInfo.applicationInfo;
if (applicationInfo == null) return null;
if (applicationInfo.dataDir == null) return null;
return new File(applicationInfo.dataDir);
} catch (NameNotFoundException ex) {
return null;
}
}
I'm not sure offhand which of those null checks are really necessary.
Then, to get the shared_prefs directory:
File shared_prefs = new File(getDataDdir(getPackageName()),
"shared_prefs"); // Except I do check for null!
So the only hardwired piece is the name "shared_prefs". Reasonably
safe, I think, but it really ought to be made an official part of the
API (or the API extended) and documented. This is an important
capability, not only for exporting, like this, but also for migrating
from a free to a shared app, etc. (I borrowed this from my upgrade
code).
I also copy files from the "files" and "databases" directories. The
latter one makes me slightly more nervous. I exclude anything matching
the regexp "webview.*", which it seems to use to manage the "cache"
directory
On Mar 25, 5:16 am, Mark Murphy <[email protected]> wrote:
> Csenteri Barna wrote:
> > Hi
>
> > I would need to export the shared preferences and load it on another
> > device. Is this possible in a direct way somehow? (without exporting
> > the keys one by one and writing to a custom file)
>
> > Another question is that there is any XML parser which has the same
> > functionality like SharedPreferences (getstring/addstring, getint/
> > addaddint etc.) and saves/loads the result to a path anywhere? (sdcard
> > for example)
>
> > I know SharedPreferences uses some xml's in the background but that
> > xml is not readable as a file without admin rights as far as I know so
> > it's not quite suitable for what I need.
>
> AFAIK, you can read the SharedPreferences as an XML file. The dangerous
> part is that you will probably have to hand-assemble the path to the
> file, as I don't recall an API to get the path for a SharedPreferences
> file from the SharedPreferences object.
>
> Otherwise, there is no export mechanism available that I am aware of.
>
> --
> Mark Murphy (a Commons
> Guy)http://commonsware.com|http://twitter.com/commonsguy
>
> Android Training in NYC: 10-11 April 2010:http://guruloft.com
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
To unsubscribe from this group, send email to
android-developers+unsubscribegooglegroups.com or reply to this email with the
words "REMOVE ME" as the subject.