I am really new to Android and just developed a WebView App in Android Studio (Java platform) for Android 7 and higher. The WebView is accessing an HTML page which dynamically generates string using JavaScript. I am able to feed the string to the WebView from the HTML page using instructions from this post: Download file generated by javascript in android webview
I would like to save the string in a text file. I tried few solutions but nothing worked. It would be really helpful if anybody could provide me some code to save the string in a .txt file. Also I need instructions on how to implement check/request permission to access storage (which may be required for saving file).
I tried to implement suggestions from "glzlaohuai" below. Below is my 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);
}
}
}
}
The code runs and I am getting the desired string in the Log, but no file is being saved or downloaded. Also I don't see any error.
Below is the modified code which is working as intended.
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();
}
}