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文件。代码使用数据创建文件,但在浏览SD卡时我无法在Windows中看到该文件。 我看到this视频并按照说明操作,但它不适用于我。也许fw变量不适合定义文件?
File csv = new File (Environment.getExternalStorageDirectory(), "DataLog.csv");
MediaScannerConnection.scanFile(
MainActivity.this,
new String[] {csv.getAbsolutePath()},
null, null);
我尝试过这样的建议,但它仍然无所事事。
答案 0 :(得分:0)
toString()
上的 FileWriter
在传递给scanFile()
的第二个参数中没有返回文件的路径(您假设它是这样的)。