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();
    }
}

我尝试了将数据写入 sd 根目录中的 DataLog.csv 文件的代码。该代码使用数据创建文件,但在浏览 sdcard 时我无法在 Windows 中看到该文件。我看到了这个视频并按照说明进行操作,但它对我不起作用。也许 fw 变量不好定义文件?

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

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

我像这样尝试过你的建议,但它仍然什么也没做。

4

1 回答 1

0

toString()onFileWriter不会在传递给的第二个参数中返回文件的路径,您假设它确实如此scanFile()

于 2013-07-31T13:36:50.323 回答