我正在尝试将数据从应用程序保存到内部存储并加载回来,但我觉得我错过了一些内容并且在加载中找不到该文件。
保存代码:
private void saveScanData(List<MyStack> surf) throws IOException { Log.i(TAG, "Saving"); String filename = String.format("Scan%05d.data", scanNumber); scanNumber += 1; ObjectOutput out; File outFile = new File(Environment.getExternalStorageDirectory(), filename); out = new ObjectOutputStream(new FileOutputStream(outFile)); out.writeObject(surf); out.close(); }加载代码:
public void load(View view) { ObjectInput in; List<MyStack> surf= null; try { in = new ObjectInputStream(new FileInputStream("Scan%05d.data")); surf= (List<MyStack>) in.readObject(); in.close(); } catch (Exception e) {e.printStackTrace();} Model model= new Model(surf); Intent intent = new Intent(this, EditorPresenter.class); intent.putExtra("model", model); startActivity(intent); }感谢您的任何帮助。
I'm trying to save data from the app to internal storage and load it back, but I feel like I miss something and the file is not found in the load.
Save code:
private void saveScanData(List<MyStack> surf) throws IOException { Log.i(TAG, "Saving"); String filename = String.format("Scan%05d.data", scanNumber); scanNumber += 1; ObjectOutput out; File outFile = new File(Environment.getExternalStorageDirectory(), filename); out = new ObjectOutputStream(new FileOutputStream(outFile)); out.writeObject(surf); out.close(); }Load code:
public void load(View view) { ObjectInput in; List<MyStack> surf= null; try { in = new ObjectInputStream(new FileInputStream("Scan%05d.data")); surf= (List<MyStack>) in.readObject(); in.close(); } catch (Exception e) {e.printStackTrace();} Model model= new Model(surf); Intent intent = new Intent(this, EditorPresenter.class); intent.putExtra("model", model); startActivity(intent); }Thank you for any help.
最满意答案
使用open openFileOutput和openfileInput方法而不是打开新的输入和输出流。
http://developer.android.com/intl/es/guide/topics/data/data-storage.html#filesInternal
此外,您正在尝试将文件保存在ExternalStorage文件夹中。 Environment.getExternalStorageDirectory() 。 在该文件夹中写入需要AndroidManifest上的Android权限
Use the open openFileOutput and openfileInput method instead of opening new Input and output stream.
http://developer.android.com/intl/es/guide/topics/data/data-storage.html#filesInternal
Moreover, you are trying to save the file in the ExternalStorage folder. Environment.getExternalStorageDirectory(). Writting in that folder requires Android permissions on the AndroidManifest
更多推荐
发布评论