【问题标题】:Nexus S Text File StorageNexus S 文本文件存储
【发布时间】:2011-06-20 21:31:43
【问题描述】:

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

  }
  }

【问题讨论】:

  • 在这个问题中添加 Android 标签会很有帮助。

标签: android text-files nexus-s android-file


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2012-11-07
    • 1970-01-01
    • 2017-05-19
    • 2017-06-16
    • 2021-02-27
    • 2018-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多