Hello, you need to put the permission in the Manifest:
<uses-permission android:name="android.permission.INTERNET" />
Still in Manifest, but inside application, add:
android:usesCleartextTraffic="true"
Inside your Activity, before webview.loadUrl... add:
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setAppCacheEnabled(true);
Edit1:
Based on this link Detect the click of the Button
To do the button processing to recover the generated image and save it create a new Class
and create the following methods:
public static String parseBase64(String base64) {
try {
Pattern pattern = Pattern.compile("((?<=base64,).*\\s*)",Pattern.DOTALL|Pattern.MULTILINE);
Matcher matcher = pattern.matcher(base64);
if (matcher.find()) {
return matcher.group();
} else {
return "";
}
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
public static void downloadFileFromBase64(String fileContent) {
try {
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "Camera");
file.mkdirs();
if (fileContent != null) {
String attachment = parseBase64(fileContent);
byte[] byteArr = Base64.decode(attachment, Base64.DEFAULT);
File f = new File(file.getAbsolutePath(),"sample.png");
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(byteArr);
fo.close();
}
}catch (Exception e){
e.printStackTrace();
}
}
These two methods parse the Base64 and save the image in the camera directory of the mobile phone.
In the Activity that is webview, below webview.loadUrl... add:
webview.addJavascriptInterface(new Object() {
@JavascriptInterface // For API 17+
public void performClick(String strl)
{
//ação para o clique do botão "save"
downloadFileFromBase64(strl);
}
}, "save");
To succeed in saving the image you need to give permissions in Manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Even so it is important to check if permission has been given, so in your Activity create the following method:
public static boolean hasPermissions(Context context, String... permissions) {
if (context != null && permissions != null) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
}
return true;
}
Still on Activity, but inside onCreate(); add:
String[] permissions = {
//aqui pode acrescentar todas as permissões que estão no Manifest
android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
};
if (!hasPermissions(this, permissions)) {
ActivityCompat.requestPermissions(this, permissions, 1);
}
IMPORTANT:
To do this, I changed your html by following the link which I based, because as I had spoken I do not understand javascript, follows the modified html:
https://jsfiddle.net/wxjkb4Lt/3/
I edited my answer, managed to treat the click of the button, recover the image and save it in the storage of the cell phone. I hope I helped, or clarified some points. Anything just send message.
– Murillo Comino