My Code:
web.loadData("<html><body> " + result.getHtmltext().replaceAll("<span class=\"stl_23 stl_10\" style=\"word-spacing:0em;\">-------------------------------</span>",
"<img height=\" 500\" width=\"500\" src=" + " \" " + "file:///android_asset/logo_transparent.png" + "\" " + "/>") + "</body></html>", "text/html; charset=UTF-8", null);
But doesn't work:
You can give it a try
String htmlData = "<body>" + "<img src=\"logo_transparent.png\"/></body>";
webView.loadDataWithBaseURL("file:///android_asset/",htmlData , "text/html", "utf-8",null);
with this, it will start picking up images from the assets folder directly.
Related
One of the activities in my app has a map on it.
I'm using Leaflet as my map and in order to use it I used WebView.
Then, In my code I use:
String Map_HTML = "<html>\n" +
"<head>\n" +
"\n" +
" <title>Quick Start - Leaflet</title>\n" +
"\n" +
" <meta charset=\"utf-8\" />\n" +
" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n" +
TONS OF HTML HERE
" color: 'red',\n" +
" fillColor: '#8275FE',\n" +
" fillOpacity: 0.3,\n" +
" weight: '0'\n" +
"}).addTo(mymap);\n" +
"\n" +
"\n" +
"var popup = L.popup();\n" +
"\n" +
"</script>\n" +
"\n" +
"</body>\n" +
"</html>";
WebView MapView = (WebView) findViewById(R.id.wb_map);
WebSettings webSettings = MapView.getSettings();
webSettings.setJavaScriptEnabled(true);
MapView.loadData(Map_HTML, "text/html", null);
In the end of the script I use webSettings.setJavaScriptEnabled(true); which gives a warning saying:
Using setJavaScriptEnabled can introduce XSS vulnerabilities into your application, review carefully.
From what I read, some people got messages from play store that asks them to update the app so it won't have this.
Since my goal to publish the app soon, I was wondering if there is an option to run it without this line?
I tried to remove it but then it just didn't show anything in the WebView.
Thank you
add this line
#SuppressLint("SetJavaScriptEnabled")
before
webSettings.setJavaScriptEnabled(true);
Documentation
I have a utility which creates daily reports as Excel sheets containing the file processed details the failed ones and then sends out a mail to users and this code is written in Java.
When the value of the failed files is 0 the mail has a blank sent instead of 0.
This is the sample code
//sample piece of code
String bodyText = "<html>"
+ "<body style =\"font-family: Calibri; font-size:11pt; background-color:white \">"
+ "<table style =\"border:1px solid black;background-color:#DBE5F1;width:100%\">"
+ "<tr style = \"background-color:#DBE5F1 ;text-align-left; font-size:11pt\">"
+ "<br>"
+ "  Hi All,"
+ "</br>"
+ "<br>"
+ statusLine
+ "</br>"
+ " "
+"<br>"
+ "</tr>"
+ "<td style = \"text-align:right;width:10%;border-right:1px solid black\">"
+ caseCreated
+ "</td>"
+ "<td style = \"width:10%;border-right:1px solid black;text-align:right\">"
+ Failure
+ "</td>"
+"</html>";
//and for composing the mail message
message.setContent(bodyText, "text/html");
I would want to send statistical information to my clients showing the number of transactions processed on every terminal or branch. I am using Apache Commons Email to send HTML emails.
I would like to send a pie-chart data like this one on the site.
My java code is basic extracted from.
It goes like:
public void testHtmlEmailPiechart()
throws UnsupportedEncodingException, EmailException, MalformedURLException {
HtmlEmail email = new HtmlEmail();
email.setHostName(emailServer);
email.setSmtpPort(587);
email.setSSLOnConnect(true);
email.setAuthentication(userName, password);
email.setCharset(emailEncoding);
email.addTo(receiver, "Mwesigye John Bosco");
email.setFrom(userName, "Enovate system emailing alert");
email.setSubject("Conkev aml Engine Statistics");
URL url = new URL("https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcROXe8tn1ljtctM53TkLJhLs6gEX56CvL0shvyq1V6wg7tXUDH8KRyVP30");
// URL url = new URL("http://www.apache.org/images/asf_logo_wide.gif");
String cid2 = email.embed(url, "logo.gif");
email.setHtmlMsg("<html>\n" +
" <head>\n" +
" <script type=\"text/javascript\" src=\"https://www.gstatic.com/charts/loader.js\"></script>\n" +
" <script type=\"text/javascript\">\n" +
" google.charts.load(\"current\", {packages:[\"corechart\"]});\n" +
" google.charts.setOnLoadCallback(drawChart);\n" +
" function drawChart() {\n" +
" var data = google.visualization.arrayToDataTable([\n" +
" ['Task', 'Hours per Day'],\n" +
" ['Work', 11],\n" +
" ['Eat', 2],\n" +
" ['Commute', 2],\n" +
" ['Watch TV', 2],\n" +
" ['Sleep', 7]\n" +
" ]);\n" +
"\n" +
" var options = {\n" +
" title: 'My Daily Activities',\n" +
" is3D: true,\n" +
" };\n" +
"\n" +
" var chart = new google.visualization.PieChart(document.getElementById('piechart_3d'));\n" +
" chart.draw(data, options);\n" +
" }\n" +
" </script>\n" +
" </head>\n" +
" <body>\n" +
" <div id=\"piechart_3d\" style=\"width: 900px; height: 500px;\">Piechart Data</div>\n" +
" </body>\n" +
"</html>");
email.setTextMsg("Your email client does not support HTML messages");
email.send();
}
My guess is that the JavaScript is not recognized because the code works like sending images,styling fonts and I have sent to my email address some sample mail. I would like your help or recommendation of any material I can read to achieve this as long as am using Java.The processes is automated running in the background so no user interface is involved.
Thanks.
I have http body in which I want to remove all the image tags. For example from a response which has the entity:
StringEntity entity = new StringEntity(
"<!DOCTYPE html>\n" +
"<html>\n" +
"<head>\n" +
"<meta charset=\"UTF-8\">\n" +
"<title>Title of the document</title>\n" +
"</head>\n" +
"<body>\n"
+ "Content of the document..\n" +
"<img src=\"image\">\n" +
"</body>\n" +
"</html>");
I tried the following but it didn't change:
String responseString = EntityUtils.toString(response.getEntity(), "UTF-8");
responseString.replaceAll("<img .*?>","");
I'm trying to implement a code editor in JavaFx using
https://gist.github.com/jewelsea/1463485
but the issue is that it downloads the CSS/Javascript from the internet to load CodeMirror. Is there a way to download those files and put them so that it can load them locally on Netbeans? I'm experienced with Java but not so much JavaFX or HTML.
I
Please put the CSS/Javascript in the classpath and use the following code:
private final String editingTemplate =
"<!doctype html>" +
"<html>" +
"<head>" +
" <link rel=\"stylesheet\" href=\"codemirror.css\">" +
" <script src=\"codemirror.js\"></script>" +
" <script src=\"clike.js\"></script>" +
"</head>" +
"<body>" +
"<form><textarea id=\"code\" name=\"code\">\n" +
"${code}" +
"</textarea></form>" +
"<script>" +
" var editor = CodeMirror.fromTextArea(document.getElementById(\"code\"), {" +
" lineNumbers: true," +
" matchBrackets: true," +
" mode: \"text/x-java\"" +
" });" +
"</script>" +
"</body>" +
"</html>";
I am just trying to pick the CSS/Javascript from the classpath instead of the internet