1

I am trying to write a .csv file to a micro SD card I have connected to my android Digiland tablet. Whenever I try to write a file to the SD card, the only thing that shows up on the SD card when I plug it into my computer is an empty folder named lost.dir. I have done some research on this and it sounds like lost.dir is a folder created when some data is lost. I am not sure why the data is being lost. I make sure to properly mount and unmount the SD card, and I am at a loss as to what to do to fix this problem. Here is the code I am using:

String string =
            " \n" + teamNum + "," + matchNum + ","+ gearPoints + "," + climbScored
                    + "," + ballsHigh + "," + ballsLow + "," + gearsAuto + "," +
            hiBallsAuto + "," + lowBallsAuto + "," + driveForward + "";

    if(isSdWriteable()) {
        File sdCard = Environment.getExternalStorageDirectory();
        File file = new File(sdCard, "scout9000.csv");

        try {
            FileOutputStream outputStream = new FileOutputStream(file);
            outputStream.write(string.getBytes());
            outputStream.close();
        } catch (Exception e) {
            System.out.println("Error writing file.");
        }
    }

Here is my isSdWriteable function:

public boolean isSdWriteable() {
    String state = Environment.getExternalStorageState();
    if(Environment.MEDIA_MOUNTED.equals(state)) {
        return true;
    }
    return false;
}

Any help is appreciated.

spam spam
  • 11
  • 1

1 Answers1

0

Environment.getExternalStorageDirectory() does not mean your external SD card. See API ref here:

Note: don't be confused by the word "external" here. This directory can better be thought as media/shared storage. It is a filesystem that can hold a relatively large amount of data and that is shared across all applications (does not enforce permissions). Traditionally this is an SD card, but it may also be implemented as built-in storage in a device that is distinct from the protected internal storage and can be mounted as a filesystem on a computer.

Getting your external SD card is more complicated - see existing question here: How can I get external SD card path for Android 4.0+?

Community
  • 1
  • 1
Tom
  • 6,946
  • 2
  • 47
  • 63