0
public void onClick(View v) {
    // Writing data to file
    FileWriter fw;
    try {
        fw = new FileWriter(Environment.getExternalStorageDirectory()+"/DataLog.csv", true);

        BufferedWriter br = new BufferedWriter(fw);
        br.append(formattedDate + String.valueOf(location.getLatitude()) + 
                ";" + String.valueOf(location.getLongitude()) + 
                ";" + String.valueOf(location.getSpeed()) + 
                ";" + String.valueOf(location.getBearing()) + 
                ";" + String.valueOf(location.getAltitude()) + 
                ";" + String.valueOf(location.getAccuracy()));
        br.append("\r\n");
        br.close();
        fw.close();

        // MediaScanner scans the file

        MediaScannerConnection.scanFile(MainActivity.this, new String[] {fw.toString()} , null, new MediaScannerConnection.OnScanCompletedListener() {

                @Override
                public void onScanCompleted(String path, Uri uri) {
                Toast t = Toast.makeText(MainActivity.this, "Scan comlete", Toast.LENGTH_LONG);
                t.show();
                }
                } );
    } catch (IOException e) {
        e.printStackTrace();
    }
}

I tried a code to write data to a DataLog.csv file in the sd root. The code creates the file with the data but i cannot see the file in windows when browsing the sdcard. I saw this video and followed the instructions but it is not working for me. Maybe the fw variable is not good to define the file?

File csv = new File (Environment.getExternalStorageDirectory(), "DataLog.csv");

                    MediaScannerConnection.scanFile(
                            MainActivity.this,
                           new String[] {csv.getAbsolutePath()},
                            null, null);

I tried your advice like this but it still doing nothing.

1 Answers1

0

toString() on FileWriter does not return the path to the file, which you are assuming it does, in the second parameter you pass to scanFile().

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thank you! I tried this: `Environment.getExternalStorageDirectory()+"DataLog.csv"` It not worked too. I am a "new guy" in pogramming. Can you tell me how can i tell the path to the scanfile? – user2628458 Jul 31 '13 at 14:18
  • @user2628458: I would not use concatenation to create a file path. Use the proper `File` constructor: `new File(Environment.getExternalStorageDirectory(), "DataLog.csv")`. You can then call `getAbsolutePath()` to get the path as a string. Also, bear in mind that Windows sometimes caches the file data, so you may need to unmount and remount your device as a Windows volume to see changes. – CommonsWare Jul 31 '13 at 17:52
  • Please look at the question's bottom. Am i doing it wrong? I use Android 2.3.3 it can be a problem? – user2628458 Jul 31 '13 at 18:27
  • @user2628458: What you have should be OK. You might consider syncing, to see if that helps, though usually that is not needed on 2.x: http://android-developers.blogspot.mx/2010/12/saving-data-safely.html And, again, the problem may be with Windows -- unmount and remount the device to see if it shows up. – CommonsWare Jul 31 '13 at 20:57
  • I put an OnScanCompletedListener() to see if it is worked but it never showed me the result and i remounted the card every time. But you were right! I put the card to my printer's card reader and now i can see the file. Thank you for your help! – user2628458 Jul 31 '13 at 21:46