1

I have this method for writing some logged data to my sd card. However it doesn't even create a file.

 public void writeToFile(LinkedList<double[]> data, String name) throws IOException {
    String dataS = "";
    dataS += "x;y;z;size\n";
    for (int i = 0; i < data.size() - 1; i++) {
        dataS += data.get(i)[0] + ";" + data.get(i)[1] + ";" + data.get(i)[2] + ";" + data.get(i)[3] + "\n";
    }

    try {
        String filename = name + ".csv";

        File myFile = new File(Environment
                .getExternalStorageDirectory(), filename);

        if (!myFile.exists()) {
            myFile.createNewFile();
        }

        FileOutputStream fos;

        System.out.println(myFile.getAbsolutePath());
        byte[] dataByte = dataS.getBytes();
        try {
            fos = new FileOutputStream(myFile);
            fos.write(dataByte);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    } catch (IOException e) {
        e.printStackTrace();
    }
}

I have added the right rights in the Manifest.xml:

 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
user3742929
  • 360
  • 3
  • 17
  • 1
    You're catching an exception and doing `e.printStackTrace` - did you check if this was happening? Is there anything in the logcat? – Aleks G Dec 19 '16 at 13:34
  • I did, no exception is printed. – user3742929 Dec 19 '16 at 13:35
  • My suggestion: put a breakpoint in your code and step through it – Aleks G Dec 19 '16 at 13:35
  • 1
    what's the output of `System.out.println(myFile.getAbsolutePath())` ? – Yazan Dec 19 '16 at 13:36
  • /storage/emulated/0/com.mbientlab.metawear.MetaWearBleService$DefaultMetaWearBoardAndroidImpl@3935adca.csv – user3742929 Dec 19 '16 at 13:37
  • So it seems liek it's creating something, but I can't find the file. – user3742929 Dec 19 '16 at 13:41
  • ok 2 things the 1- path does not look ok at least the file name, i don't think `@` and `$` are legal characters in a file name, 2- `createNewFile()` should return a boolean , check if it's true or not i think it will be false because of point 1. – Yazan Dec 19 '16 at 13:42
  • change the filename to "test.csv", createNewFile() returns true, still can't find the file anywhere – user3742929 Dec 19 '16 at 13:47
  • well, based on your prev. comment, it should be here `/storage/emulated/0/` ?! – Yazan Dec 19 '16 at 13:50
  • one more thing, `Environment .getExternalStorageDirectory()` should not necessarly be the SD card it's whatever is set to be external storage, check this question http://stackoverflow.com/questions/6049114/environment-getexternalstoragedirectory-does-not-return-the-path-to-the-removabl – Yazan Dec 19 '16 at 14:14

1 Answers1

1

Make file in the root directory like this.

File myFile = new File(Environment.getExternalStorageDirectory()+"/"+filename);

Remove this code

if (!myFile.exists()) {
   myFile.createNewFile();
}
Jinesh Francis
  • 3,377
  • 3
  • 22
  • 37
  • Still doesn't create a file. – user3742929 Dec 19 '16 at 13:50
  • i don't think this will help, we have a problem in file name `com.mbientlab.metawear.MetaWearBleServic‌​e$DefaultMetaWearBoa‌​rdAndroidImpl@3935ad‌​ca.csv` is not legal and `FileNotFoundException` will be thrown once the `FileOutputStream` is being initialized. – Yazan Dec 19 '16 at 13:52