I have read several topics here on SO about this, at first all of them seem related but than when i read the "solution" code i cannot understand where the String that keeps the code is.
What i want to do is load a local HTML file, than modify it with some javascript. And after i have modified it i would like to either replace the unmodified HTML file with the modified HTML file, or create a new HTML file from the modified HTML. So after this process i would have the modified HTML file saved on the users SD card.
I would have loved it if there where a functions like this:
String htmlContent = myWebView.getSouce();
Than i could just create an HTML file from that String and save it to the sd card.
This is my code so far.
final WebView webview = (WebView)rootView.findViewById(R.id.webView);
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url)
{
/* This loads a javascript to the Html file and changes its design*/
view.loadUrl("javascript:(function() { "+
"Some Modifications to the test.html file"+
"})()");
//Now when i have modified the above test.html i would like
//to get the modified HTML (The HTML now displaying in my webview).
//So i was hoping i could write something like this:
// String htmlContent = view.getSouce();
}
});
webview.loadUrl("file:///android_asset/test.html");
If you don't understand javascript interfaces:
http://developer.android.com/reference/android/webkit/WebView.html#addJavascriptInterface(java.lang.Object, java.lang.String)
http://developer.android.com/guide/webapps/webview.html#BindingJavaScript
Related
I load data from html to web view and there are link some where of my data that load in web view and i want when i click them there should be function call as complete action using.
So i can load that link in other net browser.
Try this one:
myWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
Ref. from: Link should be open in same web view in Android
Keep Googling :)
Here is sample html for example:
<html>
<body>
This is some string
</body>
</html>
Save this html file as "filename.html" on your desktop
Now copy that file and paste into your Assets folder
After that go to your Java code and write code:
web1.loadUrl("file:///android_asset/filename.html");
EDITED
Here is another way if you want dynamic value as content of your html see below
String d = "your dynamic string value";
String your_html = "<html><body>"+d+"</body></html>";
web1.loadData(your_html, "text/html", "UTF-8");
I have a locally stored HTML file in my assets folder that I am accessing through this:
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Global.setUpButton(this);
WebView webView = new WebView(this);
setContentView(webView);
//Sets up the settings for the webView
webView.loadUrl("file:///android_asset/map.html");
webView.getSettings().setLoadsImagesAutomatically(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.setInitialScale(0);
}
I was wondering if there is any way I would be able to view and/or edit the source of the HTML file loaded, as I may need to manipulate certain elements.
I found a workaround instead.
Created a JavascriptInterface that will allow me to append any HTML tags I need to. This is easier because I won't have to reload the page, which reduces loading time.
Sure. You create a layout with an EditText box, you load the file and display it in the box, then after editing, you store the new string in the file.
added:
You can use AssetManager to read the data as a String from the URL. Then you put the string in the EditText. Then the user edits it. Then you get the edited html from the editText, and do what you wanted to do with it in the first place. If I understand your questions correctly.
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 + "\"
I am having difficulty in loading a html file from my project assets folder into a webview. I have looked at dozens of tutorials and solutions but none seem to work for me.
In my project's assets folder I have two simple html files. index.html and faq.html
(The plan is to utilise this structure for my help documentation)
My code:
WebView wv = (WebView)findViewById(R.id.webview1);
wv.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
});
wv.loadUrl("file:///android_asset/index.html");
The webview displays the following:
Web Page Not Available
The Web Page at file:///android_asset/index.html could not be loaded as:
The requested file was not found. index.html
From everything I have read what I have here should work, but it does not.
your usage is right, so if has this problem, you need check the index.html file existed or not carefully, also you can clean the project, and rebuild it.
You can try this code ....
WebView myBrowser;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String myURL = "file:///android_asset/index.html";
myBrowser=(WebView)findViewById(R.id.mybrowser);
/*By default Javascript is turned off,
* it can be enabled by this line.
*/
myBrowser.getSettings().setJavaScriptEnabled(true);
myBrowser.setWebViewClient(new WebViewClient());
myBrowser.loadUrl(myURL);
}
I want to create a simple web based application on Android that loads certain large files, like Javascript and user interface elements locally, from 'assets' directory. However I am unable to use local assets directory when loading external HTML. So the external server would give something like this:
<script type="text/javascript" src="file:///android_asset/javascript.js"/>
And WebView would use that, even if the website itself came from external website. I know there are security risks involved, but is there a way to perhaps do that by only allowing specific URL's on applications end?
Is there any way I can do that?
First of all you have to copy all the assets somewhere (external storage, for instance).
Then, to filter URL's you should override method
public boolean shouldOverrideUrlLoading(WebView view, String url);
in WebViewClient class.
added:
When your application starts, put the assets to /sdcard/somefolder/ see getExternalFilesDir()
in shouldOverrideUrlLoading override url to something like this
file:///sdcard/somefolder/<your filename here>
it should be as easy as
public boolean shouldOverrideUrlLoading(WebView view, String url) {
url = "file://" + context.getExternalFilesDir().getAbsolutePath() + "/" + url;
super.shouldOverrideUrlLoading(view, url);
}