0

我想保存一个文件,其中包含一个来自edittext的字符串,然后加载它。

不知何故,这不起作用。我认为存储的文件找不到或没有存储(根据日志)。

如何解决这个问题?

这是保存代码:

Log.i("Watcher","Saving...");
ProgressDialog dSave = ProgressDialog.show(this, "Saving", "SAving. Please wait...",false);             
String fName = "WatchConf";
EditText servPath = (EditText)findViewById(R.id.ServerPath);
String sServPath = servPath.getText().toString();

try {
    FileOutputStream fos = openFileOutput(fName, Context.MODE_PRIVATE);
        fos.write(sServPath.getBytes());
        fos.flush();
        fos.close();
        Log.d("Watcher","Saved");

        File fCheck = new File(getFilesDir()+fName);
        if(fCheck.exists()){
            Log.i("Watcher","Saved successfully");
        }

加载代码:

Log.i("Watcher","Loading...");
        String fName = "WatchConf";
        EditText servPath = (EditText)findViewById(R.id.ServerPath);

        try {           
            InputStreamReader isr = new InputStreamReader(openFileInput(getFilesDir()+"/"+fName));
            char[] cRead = new char[100];
            isr.read(cRead);
            String sRead = new String(cRead);
            servPath.setText(sRead);
            isr.close();
            Log.i("Watcher","Loaded");
4

2 回答 2

1

你写过:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
于 2012-04-25T13:23:09.277 回答
0

它不起作用的原因是,我想将字符串过早加载到视图中。

setContentView(R.layout.main);解决问题后加载。

于 2012-04-26T08:48:06.083 回答