为什么写入SD卡的代码在一台设备上工作,而在另一台设备上却没有?

时间:2011-08-05 18:59:11

标签: android

这是我的代码:永远不会在SDCARD上创建文件

if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))
{
  logger.info("There is no external SD card mounted...stopping application");
  Toast.makeText(MonitorService.this, "There is no external SD card mounted...stopping application", Toast.LENGTH_SHORT).show();
  return;
}

final File root = Environment.getExternalStorageDirectory();
final File binaryfile = new File(root, "test.log");

      writer = new FileWriter(binaryfile, true);
      out = new BufferedWriter(writer);

      if (root.canWrite())
      {
         System.out.println("I do see this in the logs....");
         out.write("This is a test");
         out.write("\n");
         out.close();
         writer.close();
      }

1 个答案:

答案 0 :(得分:2)

SD卡写入仅在未安装在计算机上时才有效:是自动安装SD卡的设备之一吗?我见过一些自动挂载SD卡的供应商附加服务。一般情况下,我希望root.canWrite()能正常工作,但您可能需要使用getExternalStorageState来验证SD卡确实可用。