Nexus S 文本文件存储
我正在尝试从我的一个传感器读取数据并将其写入文本文件。我正在使用 Eclipse,在 Samsung Nexus S 上运行我的项目。我想收集数据,将其存储到文本文件中,然后在通过 USB 电缆将 Nexus S 连接到桌面时访问该文件。
我看过很多解释;但是,他们要么解释如何将文件保存到 Nexus S 没有的 SD 卡,要么解释 Android 不允许用户访问应用程序数据。我想避免root手机。
我一直在使用两种主要的示例方法,请记住其中一种方法已被注释掉:
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){
}
}
I'm trying to read data from one of my sensors and write it to a text file. I'm using Eclipse, running my project on a Samsung Nexus S. I want to gather data, store it into a text file, and then access that file when I attach the Nexus S to my desktop via a USB cable.
I've seen a lot of explanations; but, they either explain how to save files to an SD card, which the Nexus S does not have, or they explain that Android does not allow users to access apllication data. I want to avoid rooting the phone.
There were two primary example methods that I have been toying with, keep in mind that one of the methods is commented out:
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){
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Nexus S 有一个“/sdcard/”路径,就像大多数手机一样。您可以通过
new File("/sdcard/");
访问它,但首选方法是使用new File(Environment.getExternalStorageDirectory());
。该命令为您提供外部存储位置,以防特定手机没有描述的“/sdcard”路径此处。The Nexus S has an "/sdcard/" path just like the majority of the phones out there. You can access it through
new File("/sdcard/");
but the preferred way to do it is withnew File( Environment.getExternalStorageDirectory());
. That command gives you the external storage location, in case a particular phone does not have the "/sdcard" path as described here.