0

我正在尝试从我的一个传感器读取数据并将其写入文本文件。我正在使用 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){

  }
  }
4

1 回答 1

2

Nexus S 有一个“/sdcard/”路径,就像那里的大多数手机一样。您可以通过它访问它,new File("/sdcard/");但首选方法是使用new File( Environment.getExternalStorageDirectory());. 该命令为您提供外部存储位置,以防特定手机没有此处所述的“/sdcard”路径。

于 2011-06-20T22:09:49.410 回答