如何在WebView中访问html表单数据?

时间:2016-11-17 07:35:07

标签: android html assets

我创建了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();
    }
}

0 个答案:

没有答案