我创建了WebView
并使用html
个包作为源,但我需要将数据保存在我的设备的<filename>.txt
文件中。我试过以下代码,但无法访问表单提交。它只是使用android Environment.getExternalStorageDirectory()
制作文件夹。有什么建议吗?
public void saveData(String Body) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM_dd");
Date now = new Date();
String fileName = "Report_" + formatter.format(now) + ".txt"; //like 2016_01_12.txt
try {
File folder = new File(Environment.getExternalStorageDirectory(), "DoveHairData");
if (!folder.exists()) {
folder.mkdirs();
}
File gpxfile = new File(folder, fileName);
if (!gpxfile.exists()) {
gpxfile.createNewFile();
}
FileWriter writer = new FileWriter(gpxfile, true);
writer.append("\n\r" + Body);
writer.flush();
writer.close();
Intent mediaScanIntent = new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE");
Uri contentUri = Uri.fromFile(gpxfile);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
} catch(IOException e) {
e.printStackTrace();
}
}