I need to load the HTML content which includes, remote image src.
So I wrote a string like below
String trl = "<html><body><a id=\"example1\" href=\"http://example.com/android/images/4252054277_f0fa91e026.jpg\"><img alt=\"example1\" src=\"http://example.com/android/images/4252054277_f0fa91e026_m.jpg\" /></a></body></html>";
I then load with the below URL
webview.loadDataWithBaseURL("", trl, "text/html", "UTF-8", "");
It's showing the text i.e "example-1" instead of showing the image from the URL. I double check the location of images too. they are valid.
What am I doing wrong?
Related
So I was going to loadData() into my WebView with my HTML in the type of String that contains a direct download URL, shown below:
loadData(htmlFilesInString, "text/html", "utf-8")
Where the download URL on the HTML like this:
src="https://firebasestorage.googleapis.com/v0/b/valostats-657c2.appspot.com/o/agents%2Fa3bfb853-43b2-7238-a4f1-ad90e9e46bcc%2Fa3bfb853-43b2-7238-a4f1-ad90e9e46bcc.glb?"
Apparently, when it's being loaded the download URL is encoded where the %2F into / symbol. So it will be like this:
src="https://firebasestorage.googleapis.com/v0/b/valostats-657c2.appspot.com/o/agents/a3bfb853-43b2-7238-a4f1-ad90e9e46bcc/a3bfb853-43b2-7238-a4f1-ad90e9e46bcc.glb"
Which is produce error like this
Error code 400: Invalid HTTP method/URL pair
So, is there any way to load my HTML into webview without encode the download url?
I have String with html format, not url, that send through from api. Those include image and bootstrap css. I would like to download those things to show in offline including image and css to show right format.
Is there any library to download html string content?
All You Have to do is get your Json Data convert Json to String json.toString();
and pass it to data=json.String();
and check if data is already stored or not in Shared Preferences
DataBaseHelper DB;
if(DB.getHTMLStoredData(context)){
data=DB.getHTMLStoredData(context)
}else{
data=json.String();
}
//data == html data which you want to load
WebView webview = (WebView)this.findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadDataWithBaseURL("", data, "text/html", "UTF-8", "");
I want to implement a Rich text editor by using a webview in android. Content can be loaded by using a HTML file (which resides in assets) without any problem. But if user has edited the content of the webview (with all the formatting), I need to get the modified content as a HTML string and save it in the database. How can I do this?
I tried in many ways but it seems that we need to pass a URL to get the content of the webview. But after editing the webview content, how can we get the edited URL? or current updated webview content to HTML formatted string?
Using below code I made editable web view.
String msgBody = "<html>\n"+
"<body>\n"+
"<div id=\"content\" contenteditable=\"true\" style=\"font-family:Helvetica;font-size:14px\">" + a +" </div>\n"+
"</body>"+
"</html>";
// wbview = (WebView)this.findViewById(R.id.wbview);
wbView.getSettings().setJavaScriptEnabled(true);
wbView.loadDataWithBaseURL("", msgBody, "text/html", "UTF-8", "");
wbView.setHorizontalScrollBarEnabled(true);
wbView.setVerticalScrollBarEnabled(true);
In iOS we can get it easily by using below code line.
NSString* html=[_tbEmail.webView stringByEvaluatingJavaScriptFromString:#"document.getElementsByTagName('body')[0].innerHTML"];
In loadDataWithBaseURL method from Android WebView, there are "baseUrl" and "historyUrl".
What are they used for?
I have read the android documentation but still don't know what they are.
Loading HTML Into a WebView With a Base URL
If the HTML you load directly into the WebView in your Android web app contains links with relative URLs, then these links may not work correctly. When you load HTML directly into the WebView the HTML has no base URL from which to interpret the relative URLs. The Android WebView component has a solution for that.
You can load HTML directly into the WebView with a base URL. The base URL is then used to resolve all relative URLs in the HTML. To load HTML with a base URL you have to use the loadDataWithBaseURL() method. Here is a WebView loadDataWithBaseURL() example:
String baseUrl = "http://tutorials.jenkov.com";
String data = "Relative Link";
String mimeType = "text/html";
String encoding = "UTF-8";
String historyUrl = "http://tutorials.jenkov.com/jquery/index.html";
webView.loadDataWithBaseURL(baseUrl, data, mimeType, encoding, historyUrl);
The loadDataWithBaseURL() method takes 5 parameters. The data parameter is the HTML to load into the WebView. The mimeType is the mime type of the data loaded into the WebView (in this example text/html). The encoding is the binary encoding of the data (in this example UTF-8). Note: I tried using UTF-16 as encoding but the content displayed in the WebView looked pretty strange (like Asian characters).
The baseUrl parameter is the base URL from which all relative URLs in the loaded HTML is interpreted.
The historyUrl parameter is the URL to write into the WebView's internal navigation history for the HTML loaded into the WebView. If the user navigates from the loaded HTML to another page, and then clicks the "back" button, then it is this URL the WebView will navigate back to. You may have to intercept the loading of this URL, since navigating back the WebView's history will not take you to the loaded HTML, but to the URL specified in the historyUrl parameter (or about:blank if historyUrl is set to null).
For more information go through this tutorial and this stackoverflow answer.
What android document says :
Loads the given data into this WebView, using baseUrl as the base URL for the content.
I would like to set some html content in my webview with some pictures.
I've read on the internet i must use loadDataWithBaseUrl to do such a thing because i need to link the folder of images (basUrl) in order.
My html content nicely loads, i can even ran the javascripts perfectly, but for some reason my images cannot be loaded.
Somewhere i've read there are some security reasons and thats why i cant load images from sd card to webview and some says it can be easily done via loadDataWithBaseUrl, so i really dont know which one is true.
Here is my method i tried, maybe with some mistakes so dont be rude:
I got my html file here:
mnt/sdcard/com.mypackage.myproject/3/3.html
My images are here:
mnt/sdcard/com.mypackage.myproject/3/images/Cover.png
And this is my content loading:
myWebView.loadDataWithBaseURL("file:///mnt/sdcard/com.mypackage.myproject/3", myHtml, "text/html", "utf-8", "");
In my html code:
<img src="images/Cover.png" alt="Cover.png" height="820"/>
So as you see i give the baseUrl, and for some reason the webview cannot load my images.
As many said, this can be a solution:
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setAllowFileAccess(true);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setBuiltInZoomControls(true);
String base = Environment.getExternalStorageDirectory().getAbsolutePath().toString();
String imagePath = "file:/"+ base + "/test.jpg";
String html = "<html><head></head><body><img src=\""+ imagePath + "\"></body></html>";
mWebView.loadData(html, "text/html","utf-8");
BUT, i have 700 different html files and there are many images in many different places... so i cannot modify the html code.
Is there a way to link the image folder to my html files to properly use them as a baseUrl?
You have a syntax error in your loading statement. You have to put : after file
myWebView.loadDataWithBaseURL("file:///mnt/sdcard/com.mypackage.myproject/3", myHtml, "text/html", "utf-8", "");
You don't need to put full path of your image into html.
img src="images/test1.jpg"
Instead
img src=\""+ imagePath + "\"