开发者

Why does my code to write to the sdcard work on one device but not on another device..?

开发者 https://www.devze.com 2023-03-26 10:20 出处:网络
Here is my code: The file never gets created on the SDCARD if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))

Here is my code: The file never gets created on the 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();
  开发者_Python百科    }


The sdcard write only works if it is NOT mounted on the computer: is one of the devices automatically mounting the SD card? I've seen some of the vendor add-on services that automount the SD card. In general I'd expect root.canWrite() to work correctly, but you may want to use getExternalStorageState to verify that the SD card is indeed available.

0

精彩评论

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