如何将JavaScript生成的数据保存到Android WebView中的文件中



我真的是Android新手,刚刚在Android Studio(Java平台(中为Android 7及更高版本开发了一个WebView应用程序。WebView正在访问一个HTML页面,该页面使用JavaScript动态生成字符串。我能够使用这篇文章中的说明将字符串从HTML页面馈送到WebView:下载android WebView 中javascript生成的文件

我想把字符串保存在一个文本文件中。我尝试了几种解决方案,但都没用。如果有人能给我提供一些代码来将字符串保存在.txt文件中,那将非常有帮助。此外,我还需要有关如何实现访问存储的检查/请求权限的说明(这可能是保存文件所必需的(。

我试图实施";glzlaohuai"在下面下面是我的MainActivity.java

package com.example.brahma;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import androidx.appcompat.app.AppCompatActivity;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class MainActivity extends AppCompatActivity {
private Context mContext;
private Activity mActivity;
private static final int MY_PERMISSION_REQUEST_CODE = 123;
WebView web;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
web = findViewById(R.id.brahma_wv);
WebSettings webSettings = web.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setSupportZoom(true);
webSettings.setAllowFileAccess(true);
webSettings.supportMultipleWindows();
webSettings.setSupportZoom(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setDisplayZoomControls(false);
webSettings.setAllowFileAccessFromFileURLs(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
web.setWebViewClient(new Callback());
web.loadUrl("https://script.google.com/macros/s/myWebAppId/exec");
web.addJavascriptInterface(new myJavascriptInterface(this), "Android");
}

public class myJavascriptInterface {
Context mContext;
myJavascriptInterface(Context c) {
mContext = c;
}
@JavascriptInterface
public void sendData(String attString) {
File file = new File(getExternalFilesDir(null), "Testfile.txt");
try {
OutputStream fileWriter = new FileOutputStream(file);
Log.d("ATT", attString);
fileWriter.write(attString.getBytes());
fileWriter.close();
} catch (IOException e) {
Log.w("ExternalStorage", "Error writing " + file, e);
}
}
}
} 

代码运行,我在日志中得到了所需的字符串,但没有保存或下载任何文件。我也没有看到任何错误。

以下是按预期工作的修改后的代码。

public class myJavascriptInterface {
Context mContext;
myJavascriptInterface(Context c) {
mContext = c;
}
@JavascriptInterface
public void sendData(String attString) {
Log.d("Txt", attString.toString());
//save attString as file
String filename = "TestFile.txt";
saveTextAsFile(filename, attString);
}
}
private void saveTextAsFile(String filename, String content) {
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), filename);
try {
FileOutputStream fos = new FileOutputStream(file);
fos.write(content.getBytes());
fos.close();
Toast.makeText(this, "Saved!", Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}

您应该在Java端实现saveTheGeneratedString(使用您喜欢的任何其他名称(方法,并在javascript代码中调用此方法,有关详细信息,请参阅此链接:https://developer.android.com/reference/android/webkit/WebView#addJavascriptInterface(java.lang.Object,%20java.lang.String(

关于访问存储的权限,因为您的应用程序以Android 7及更高版本为目标,因此不需要额外的权限,请参阅以下链接了解详细信息:

https://developer.android.com/reference/android/content/Context#getFilesDir((https://developer.android.com/reference/android/content/Context#getExternalFilesDir(java.lang.String(

最新更新