我通过在webview中加载的java脚本调用本机android函数但是不起作用 这是我用WebView做的事情
paintView = (WebView) findViewById(R.id.webView1);
WebSettings Websettings = paintView.getSettings();
Websettings.setBuiltInZoomControls(true);
Websettings.setSupportZoom(true);
Websettings.setJavaScriptEnabled(true);
Websettings.setBuiltInZoomControls(true);
Websettings.setRenderPriority(RenderPriority.HIGH);
paintView.getSettings().setPluginState(PluginState.ON);
paintView.setWebChromeClient(new WebChromeClient());
final MyJavaScriptInterface myJavaScriptInterface = new MyJavaScriptInterface(this);
paintView.loadUrl("file:///android_asset/index_jQueryMobile.html");
这是android方法
@JavascriptInterface
public void byteToImage(String base64img, String ImageName) throws IOException
{
byte[] pdfAsBytes = Base64.decode(base64img.toString(), 0);
File file = new File(Environment.getExternalStorageDirectory()+"/AndroPaint");
if(!file.exists()) {
file.mkdirs();
}
File filePath = new File(Environment.getExternalStorageDirectory()+"/AndroPaint/"+ImageName+".png");
FileOutputStream os = new FileOutputStream(filePath, true);
os.write(pdfAsBytes);
os.flush();
os.close();
}
这是JS函数
function getImageBytes()
{
var canvas = document.getElementById('canvas');
var imageName = document.getElementById('imgName');
var imgData= canvas.toDataURL();
activity.byteToImage(imgData,imageName);
}
这是例外
01-04 10:48:50.409: E/Web Console(15681): Uncaught ReferenceError: activity is not defined:72
答案 0 :(得分:1)
您创建了一个连接界面MyJavaScriptInterface
,但您忘记将其附加到WebView
:
paintView.addJavascriptInterface(new MyJavaScriptInterface(this), "activity");
这就是您收到activity is not defined
错误的原因。 activity
引擎无法找到JS
个对象。
Here是一个关于
的小教程