我用原來的方法無法解決這個問題。 因此,我將下面的javascript修復添加到 html: https://code.google.com/p/todataurl-png-js/ 允許使用canvas.toDataUrl()
返回一個Base64編碼的PNG圖像。 然後我用WebView.addJavascriptInterface
允許javascript將base64編碼圖像發送到 java,然後將它的保存到設備上。
我所做的就是這樣的一個例子://After initializing the webview:
JavaScriptInterface jsInterface = new JavaScriptInterface();
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(jsInterface,"android");
//The class used as javascript interface for saving the image to a file
public class JavaScriptInterface {
@JavascriptInterface
public void canvasToImage(String base64ImageData){
String capturePathString = Environment.getExternalStorageDirectory().getPath() +"/temp/ms_" + System.currentTimeMillis() +".png";
try{
File file = new File(capturePathString);
file.getParentFile().mkdirs();
FileOutputStream fos = new FileOutputStream(file);
byte[] decodedString = android.util.Base64.decode(base64ImageData, android.util.Base64.DEFAULT);
fos.write(decodedString);
fos.flush();
fos.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
//In the javascript it looks something like this
function canvasToImage(){
var dataUrl = canvas.toDataURL();
window.android.canvasToImage(dataUrl.replace("data:image/png;base64,",""));
}
它不像我希望的那麼乾淨,但是它現在可以在這裡的所有設備上工作。