I am trying to load a local javascript file from a webview.
The file "search.js" is located under the assets folder in my project.
I used this line to load my webpage :
mWebView.loadDataWithBaseURL("file:///android_asset/", data, "text/html", "utf-8", null);
on my webpage, I am using this line :
<script type="text/javascript" src="search.js"></script>
to load my javascript file.
When I run the program, I just get a white page..............
I don't know what to do. Do you have any ideas ?
Thanks !!
Try this:
wv.loadUrl("file:///android_asset/yourfile.html");
wv.getSettings().setJavaScriptEnabled(true);
Related
I am loading my android WebView using
mywebview.loadDataWithBaseURL("file:///android_asset/", new String(result), "text/html", "UTF-8", null);
HTML rendered successfully in WebView. now I want to open another HTML file from that HTML using button for that I am using below code in HTML.
<button onclick="location.href='data/1/quiz_adjective.html'" > Start</button>
but it does not work. my asset html file directory is - assets\data\1\htmlfile.html
When you make Android applications, you can parse HTML data or HTML pages got from the Web by JSoup libraly.
Before loading url you should enable JS like this. Then, It should work. Or there is a more advanced way with JavascriptInterfacedocs
mywebview.getSettings().setJavaScriptEnabled(true);
I don't know much about html but there is a small issue and I am unable to find its solution.
This is the iframe that I want to display on static html page:
<!DOCTYPE html>
<html>
<body>
<iframe style="width:120px;height:240px; padding-right:50px; padding-bottom:50px" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" src="//ws-na.amazon-adsystem.com/widgets/q?ServiceVersion=20070822&OneJS=1&Operation=GetAdHtml&MarketPlace=US&source=ac&ref=qf_sp_asin_til&ad_type=product_link&tracking_id=qstore51214-20&marketplace=amazon®ion=US&placement=0553496670&asins=0553496670&linkId=4f9912a00b832e2f8bcb5a9b187511cf&show_border=true&link_opens_in_new_window=true&price_color=333333&title_color=0066c0&bg_color=ffffff">
</iframe>
</body>
</html>
When I add this to html and try to open html page, I get the error:
"File not found".
But when I add this iframe to any live html editor it work perfectly and show the link.
Actually I want to display this iframe in Webview in my Andriod application.
My android code is:
mWebViewTopSeller = (WebView) findViewById(R.id.webViewTopSeller);
mWebViewTopSeller.setWebChromeClient(new WebChromeClient());
mWebViewTopSeller.setWebViewClient(new WebViewClient());
mWebViewTopSeller.getSettings().setJavaScriptEnabled(true);
mWebViewTopSeller.loadUrl("file:///android_asset/TopSeller.html");
Please help. Thanks!
When embedding this iframe, it returns an error:
SEC7111: HTTPS security is compromised by https://ws-na...
So it may have something to do with the browser's mixed-content/same-origin policy.
Possible src values are an absolute URL that points to another web site (like src="http://www.example.com/default.htm")
or an relative URL that points to a file within a web site (like src="default.htm" I think your src path is wrong.
I know that for security reasons HTML standard doesn't allow anymore the loading of a local resource as image in a document.
Anyway I found that I have to copy the html files in the android-asset project folder if I want to load local pages in a WebView.
What if I want to use an hybrid approach?
I would to get a HTML document from an uri, replace the value of the src attribute of a img HTML tag with a local path and then load the code in a WebView.
Having a html document like this:
<hmtl>
<head>
</head>
<body>
<img src="http://URL-SITE/img/x.jpg">
</body>
</hmtl>
How can i replace http://URL-SITE/img with the path of a local image resource
before the page is loaded by a WebView?
I already tried putting the local image in the folder android-asset and changing the src value in file:///android_asset/img/x.jpg but it didn't work.
Ok guys, thanks for the comments.
The problem was that I used the command loadData
String html ="<html> <head></head> "
+" <body> <img src=\"file:///android_asset/img/x.jpg\"> "
+"</body> </html>";
myWebView.loadData(html, "text/html; charset=UTF-8", null);
instead of
myWebView.loadDataWithBaseURL(null, html, "text/html", "UTF-8", null);
For my android app I want to load a html file to webview, this html file will be stored in "data/data/com.myapp/files/index.html". I am able to load it with the associated javascript files but when I want to load the image from "data/data/com.myapp/files/img/image.png" it doesn't work.
This is how i'm trying to do it:
webView.loadDataWithBaseURL(getFilesDir()+"/img/", html, "text/html","utf-8", ""); //html is my html string I get from index.html
I've also tried:
webView.loadUrl(getFilesDir()+"/img/"+index.html");
In my html file i have
<img id="image" src="/img/image.png"/>
I've verified and the file is loaded on the disk but i can't display it on webview, the display stays blank for image. I just can't make it work. So my question is: is it even possible? If yes can someone please tell me how?
I've searched and found that others had similar problems but none of the solutions is working.
EDIT: Putting my files in asset directorty is not an option for me, which i've already tested and it works.
I just tried the following code I found here: Load the image saved in sdcard in webview
String base = getFilesDir();
String imagePath = base + "/test.jpg";
String html = ("<html>
<head>
</head>
<body>
<img src=\""+ imagePath + "\">
</body>
</html>
");
mWebView.loadData(html, "text/html","utf-8");
But still no success, apparently it worked for the person who asked the question. I've also tested with the external storage but the result is same. All permissions are there in manifest.xml. So I really don't see what the problem is. Any suggestion or advice will be appreciated.
Consider putting static files in assets folder inside android project, and try loading file using:
mWebView.loadUrl("file:///android_asset/html/index.html");
And then, from HTML file refer to images, styles using relative path.
For example, in your case, you would have follwing directory structure:
PROJECT-ROOT
|-src/
|-assets/
|-html/index.html
|-img/image.png
|-css/style.css
After this, you can use normal html tags inside index.html as you would do to make any web page. Eg. <img id="image" src="img/image.png"/> or <link rel="stylesheet" type="text/css" href="css/style.css">
Don't forget to enable JavaScript using:
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
Problem solved. It was actually coming from bad formatted data, once I checked and added correct files to phone memory it started working.
When you want to save image to internal storage, be sure not to use same code as downloading html file. That's the mistake I made. Instead, use
How to download and save an image in Android
to save your image, then the image should be fine
I am wondering how do I set the path for WebViews loadWithBaseURL correctly.
What I want to do is, to load html in a webview, that uses resources that are stored on the external storage.
For Example:
<html>
<head>
<style>body{ background-image:url(beach.jpg); }</style>
</head>
<body>
<img src="football.jpg" />
</body>
</html>
Where beach.jpg and ball.jpg are stored directly in the "root" directory of the phones external storage (/sdcard/beach.jpg and /sdcard/ball.jpg)
So I tried to load the content as follows:
String html = "<html> ... example from above ... </html>";
String base = Environment.getExternalStorageDirectory().getAbsolutePath().toString();
webView.loadDataWithBaseURL("file://" + base, html, "text/html", "utf-8", null);
However the path seems to be wrong, because I can't see the image in the webview.
Any suggestions?
Have you enabled file access on the webview?
webView.getSettings().setAllowFileAccess(true);
Additionally, if you are constructing the HTML yourself - you might consider using full paths for the images.
String html = "<html>... <img src=\"file://"+base+"/football.jpg\" />";
Have you give internet permission?
<uses-permission android:name="android.permission.INTERNET" />
These may also help..
Android webview loadDataWithBaseURL how load images from assets?
http://myexperiencewithandroid.blogspot.com/2011/09/android-loaddatawithbaseurl.html
Android v2.2-2.3.5: WebView : loadDataWithBaseURL : will only load page once