I have created a WebView
and using html
packages as a source but I need to save data in <filename>.txt
file in my device. I have tried following code, but can't access form filed. It's just making folder using android Environment.getExternalStorageDirectory()
. Any suggestions?
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();
}
}