Android : Couldn't create directory for SharedPreferences file

I am trying to edit SharedPreferences of one app through another app, my code goes like this

try {
Context plutoContext = getApplicationContext().createPackageContext("me.test",Context.MODE_WORLD_WRITEABLE);
SharedPreferences plutoPreferences = PreferenceManager.getDefaultSharedPreferences(plutoContext);
Editor plutoPrefEditor = plutoPreferences.edit();
plutoPrefEditor.putString("country", "India");
plutoPrefEditor.commit();
}

I am getting an error

E/SharedPreferencesImpl( 304): Couldn't create directory for SharedPreferences file /data/data/me.test/shared_prefs/me.test_preferences.xml

where me.test is my another project in me.test proj I can edit and retrieve SharedPreferences with no pain

I am testing it on Nexus S android 4.0.4 (Samsung), can any one help me


You need to add the permissions in your AndroidManifest.xml:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

I have the same problem with you, and I solved it with the above solution.


Just use a different package name in this app. That will create the preferences in a different directory.


By the way, (it's not related with the question, but any way) you wrongly use createPackageContext() method. You pass there MODE_WORLD_WRITEABLE flag which value is 2 and equals to CONTEXT_IGNORE_SECURITY flag, so this would be incorrectly interpreted by logic, IMHO.

链接地址: http://www.djcxy.com/p/62828.html

上一篇: 在Doxygen输出中更改index.html文件的位置

下一篇: Android:无法为SharedPreferences文件创建目录