在 phone 中保存 Android 应用程序数据

Saving Android App Data in phone

I am having trouble viewing the data from my app. I am basically 运行 an app and want the data to be saved in an text file. My code for saving is:

try {
       FileOutputStream  out = openFileOutput(FILENAME, Context.MODE_APPEND); 
       out.write (entry.getBytes());
       out.close();
       } catch (Exception e) {
         e.printStackTrace();
       }

Where entry is the string containing the data I want to be saved. The app runs fine and I can view the data in the logs. The problem is I can not find the file in Android Device Manager -> File Manager -> Data. When I click data, it is supposed to show me the package but it is empty instead. I want to save the data and use it for processing later. Could someone please help? TIA :)

Edit1: Modified to write in the external storage instead. However, this just makes one entry and does not write more than that.

FileOutputStream outputstream;
          try {
              file1 = new File(Environment.getExternalStorageDirectory(), "MyData");
              outputstream = new FileOutputStream(file1);
              OutputStreamWriter oswriter = new OutputStreamWriter(outputstream);
              BufferedWriter bwriter = new BufferedWriter (oswriter);
              bwriter.write(entry);
              bwriter.newLine();
              bwriter.close();
              outputstream.close();
             } catch (Exception e) {
                        e.printStackTrace();
                    }

我猜你是在设备上测试这个。您无法在生产硬件上任意访问 /data/,除非是在已获得 root 权限的设备上。在模拟器上进行测试或使用 adb shell run-as 从命令行访问您应用程序的 internal storage 部分。

您要保存到的位置可能只能通过您的应用程序访问。见docs。来自文档:

内部存储:

  • 随时可用。

  • 此处保存的文件只能由您的应用程序访问。

  • 当用户卸载您的应用时,系统会从内部存储中删除您应用的所有文件。

该文件不会显示在文件资源管理器中。您仍然可以在您的应用中阅读它。