我正在尝试从我的一个传感器读取数据并将其写入文本文件。我正在使用 Eclipse,在三星 Nexus S 上运行我的项目。我想收集数据,将其存储到一个文本文件中,然后当我通过 USB 电缆将 Nexus S 连接到我的桌面时访问该文件。
我看过很多解释;但是,他们要么解释了如何将文件保存到 Nexus S 没有的 SD 卡,要么解释了 Android 不允许用户访问应用程序数据。我想避免生根电话。
我一直在玩弄两种主要的示例方法,请记住其中一种方法已被注释掉:
public void appendLog(String text){
// try{
// FileOutputStream fout = openFileOutput("log.txt", MODE_WORLD_READABLE);
// OutputStreamWriter osw = new OutputStreamWriter(fout);
// osw.write(text);
// osw.flush();
// osw.close();
// }
// catch(IOException e){
//
// }
File logFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "log.txt");
if(!logFile.exists()){
try{
logFile.createNewFile();
}
catch(IOException e){
}
}
try{
BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true));
buf.append(text);
buf.newLine();
buf.close();
}
catch(IOException e){
}
}