Local files are innaccessible from an external url using PhoneGap on Android - android

<img src="file:///android_asset/www/images/image.png" style="border: black dashed 2px"/>
This line of code works as expected on a local html file.
However when you open an external url inside the app. (For example http://www.example.com/mobile/) The same line of code doesn't show an image.
To be clear, I am trying to access local files from an external url, because there are some JS files that are huge and It will be a waste of bandwidth to download them externally.

The remote site will never be able to load an image from the file:// protocol.

As #simon said u wont be able to load local assests on external url.. At best u can use loadDataWithBaseURL which will load your html with imgs/css/js from local assets folders
something like this
webView.loadDataWithBaseURL("file:///android_res/raw/", html,
"text/html", "UTF-8", null);

Related

Image not loading on html file in android mobile

I'm trying to view my HTML file on the android mobile browser.
My HTML file has the following code:
<html>
<head>
<title>Testy</title>
</head>
<body>
<h1>Captain is Here</h1>
<img src='captain.jpg' width='30%' alt='Testy'/>
</body>
</html>
The image(captain.jpg) is also in the same folder where the HTML page is located. I'm getting the image on the computer browser but when I'm trying to open it in mobile browser image, not showing. I tried giving different file paths. How can I achieve this by giving a universal path by that a mobile that containing these HTML files should open in respective browsers and render images too?
1- your image size is too large try an image with a smaller size or resize or format your image into a smaller size.
try src-'./caption.jpg' instead of src='caption.jpg'
Opening through directry location of Html in Chrome Solve this.
For that get html file location directry in details by file manager
Example storage/emulated/0/web_folder/file.html and copy it
After that
1.Open chrome
2.type file:/// and Paste directry location
3."file:///storage/emulated/0/web_folder/file.html" Now it can run with image
Ensure that storage permission allowed to chrome
For that Open permission settings for chrome and allow storage permission
that's where mhtml and URLs get used!!!
Have you ever downloaded a page using chrome in android? if so you would have known about mhtml
MHTML, an initialism of "MIME encapsulation of aggregate HTML documents", is a web page archive format used to combine, in a single computer file, the HTML code and its companion resources (such as images, Flash animations, Java applets, and audio and video files) that are represented by external hyperlinks in the web page's HTML code
to convert your html to mhtml you may use some tools but you can also do it manually .
converting need not rename the file as index.mhtml but use all necessary files as hyperlinks
you may use image hosting sites like imgur or imgbb or even firebase if your familiar.
after you host get the link by
right-clicking image
open image in new tab
copy link
use it(link) instead of captain.jpg
Here is an example of how to get link using imgbb

Load html page on webview without using a web server

Is there a way to load an html page on a webview without using a web server?
In my android application I have a web server because the user can save the web pages he wants and he can access them offline later.
I tried the assets folder but I cannot modify it at runtime. I can just read files I´ve put there.
To load online or offline I use the methods of the webview:
browser is the webview.
browser.setWebViewClient(new client());
browser.getSettings().setJavaScriptEnabled(true);
browser.getSettings().setDomStorageEnabled(true);
browser.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
browser.getSettings().setUseWideViewPort(true);
browser.loadUrl(url);
The url can be :
http://stackoverflow.com a normal one
or
http://localhost:8080"; //the web server inside the app
or from assets folder
files:///android_asset/file.html
But how I said, the use can save many pages and see it when he wants.
Is there a method to load the page saved in internal memory on the webview?
URL =this.getApplicationContext().getFilesDir()+"/www/file.html";
browser.loadUrl(URL);
For example: webview.loadString(); ?
Something like browser.loadUrl(Uri.fromFile(f).toString()) should work, for a File object named f pointing to your desired file. Uri.fromFile() will give you a Uri with the correct scheme; toString() gives you the string representation to hand to the WebView. I use this (or a variation) to read files on external storage. AFAIK, it should work fine for files on internal storage as well.

android webview load html from server and other files from assets

im a new android developer.
My app has WebView which loads lot of data from my site (which has pics, css, JS files).only the html code is updated weekly. other files are almost never changed. so i was thinking of including these static files in android assets, loading only the html from server and other files from assets and cutting down the loading time. how do i do it.
Android WebView Javascript from assets
and as shown in the above solution i cant change the html code to load these from assets because this site will also be accessed by web users.
Is there a way to do this. Thanks in advance..
Firstly you need to have relative paths
<img src="images/someimage.png">
not
<img src="www.mysite.com/images/someimage.png">
Then you need to load the HTML code not using webBrowser, for example like here
And then you can load HTML source into WebBrowser using base URL pointing to your assets like here

Unable to load local images in android webview

i am trying to load local images in android webview by using assets folder, but when i tries to open it, it is loading only HTML but not images
In my HTML Code i have added following line
<IMG SRC="file:///android_asset/images/a.jpg">
But it is not displaying image, can anybody please help?
put your images into the Assets folders by creating separate images folder like assets/images and then in your html page provide path of your image as
<IMG src="images/test.png">
first of which url loading in webview, is it local url or http (means requires internet). If ur loading local file system url pealse check the image path properly, it should work. if loading http url trying load a local images inside it sorry its not possible. Here its cross domain issue will restrict to that.

Android displaying html file with pictures and without internet connections

I would like to load a html file (with pictures and videos) on a Android tablet, however, there is no internet connection, thus I cannot use url for the pictures and stuff....
Is there any free web servers that allow me to do so (displaying the web content at local host)?
Or is there any way I can do it by writing a android app for it?
Thanks!!
Webview's LoadURL method can take local content. using the file uri retrieving the content from your assets and render it that way. Assuming you follow the standard folder structure for a basic HTML website.
And why would you want to ask about "free web servers" if you don't even have a internet connection... All the content has to be loaded locally on the app or downloaded on the fly (assuming you don't download too much.. if its too much then you are better off packaging it in the app).
Without Internet your html page is static page.
You can store the html file in your local assets folder then:
url = "file:///android_asset/" + mName + ".html";
mWebView.loadUrl(url);
and if you also want to show the images in your web page change the source path of images in source code of html page to local folder and pate all the images to that folder.

Categories

Resources