Android:查看 SharedPreferences 文件? [英] Android: Viewing SharedPreferences file?
问题描述
出于调试目的,我需要访问我的应用程序的共享首选项文件.据我所知,我应该在/data/... 中找到这个文件,但我无法访问/data 文件夹,直到缺少权限.这是正常的吗?任何方式仍然可以访问该文件?(除了可能从应用程序内部读取它?)手机没有扎根,我也不想扎根.感谢您的任何提示!
For debugging purposes I need to access the shared preferences file of my application. As far as I know I should find this file in /data/... but I can't access the /data folder through to missing permissions. Is this normal? Any way to still access the file? (except maybe raeding it from inside the application?) The phone is not rooted and I also don't want to root it. Thanks for any hint!
推荐答案
我过去遇到过这个问题(在文件系统上没有 root 权限,但需要访问应用程序数据文件夹).如果您没有 root 设备或开发人员设备(例如 ADP1),那么您可以尝试在模拟器上运行您的应用程序,然后从 eclipse 或 DDMS 中的文件资源管理器"访问文件.
I have ran into this issue in the past (not having root permission on the file system but needing access to the applications data folder). If you don't have a rooted device or a developer device such as the ADP1 then you can try running your application on the emulator and then accessing the files from the "File Explorer" in eclipse or DDMS.
编辑#1:尝试使用 sharedPreferences 的 getAll 函数和将其保存到文件中,我会看看是否可以将样本放在一起.
EDIT #1: Try using the getAll function of sharedPreferences and saving that to a file, I will see if I can throw together a sample.
编辑#2:示例代码,从网络周围的随机样本创建,可能不是最好的方法,但我测试了它并且它确实有效.它将文件写入您的 SD 卡的根目录.确保你有
EDIT #2: Example Code, created from random samples around the net, probably not the best way to do it, but I tested it and it does work. It writes a file to the root of your sdcard. Make sure you have
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
在您的清单中设置
private void saveSharedPreferences()
{
// create some junk data to populate the shared preferences
SharedPreferences prefs = getSharedPreferences("MyPrefs", MODE_PRIVATE);
SharedPreferences.Editor prefEdit = prefs.edit();
prefEdit.putBoolean("SomeBooleanValue_True", true);
prefEdit.putInt("SomeIntValue_100", 100);
prefEdit.putFloat("SomeFloatValue_1.11", 1.11f);
prefEdit.putString("SomeStringValue_Unicorns", "Unicorns");
prefEdit.commit();
// BEGIN EXAMPLE
File myPath = new File(Environment.getExternalStorageDirectory().toString());
File myFile = new File(myPath, "MySharedPreferences");
try
{
FileWriter fw = new FileWriter(myFile);
PrintWriter pw = new PrintWriter(fw);
Map<String,?> prefsMap = prefs.getAll();
for(Map.Entry<String,?> entry : prefsMap.entrySet())
{
pw.println(entry.getKey() + ": " + entry.getValue().toString());
}
pw.close();
fw.close();
}
catch (Exception e)
{
// what a terrible failure...
Log.wtf(getClass().getName(), e.toString());
}
}
这篇关于Android:查看 SharedPreferences 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!