开发者

Nexus S Text File Storage

开发者 https://www.devze.com 2023-03-14 10:17 出处:网络
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 acc

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);
    //   开发者_JAVA百科     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){

  }
  }


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 with new 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消