Access local file in a WebView - android

My native app includes a WebView, and the WebView loads a web page off web. For example, the html is loaded from
http://dongshengcn.com/test.html
It seems any page loaded from web (instead of local) can not load any file from local device.
My question is:
Is it possible for a http://dongsheng.com/test.html loaded to a webview (as part of native app) to access file on local device?

Here are a couple of things to try:
To use local files you need to place them in your project's assets folder and invoke them using URLs such as file:///android_asset/. For example, if you add mypage.html in your assets folder, then you can invoke it in the webview with file:///android_asset/mypage.html.
Check to make sure that you have the appropriate webview permissions in your Manifest. For the webview to work correctly, you need:
<uses-permission android:name="android.permission.INTERNET" />
Take a look at the following app on Github, which as a bonus also fixes a couple of bugs with the webview in Honeycomb and ICS. It is a full example on how to use the webview with local files:
https://github.com/bricolsoftconsulting/WebViewIssue17535FixDemo
EDIT: Addendum after question clarification:
Yes, it is possible to load a local page from the web, but you must use a trick to bypass the browser's security measures.
Replace the file://android_asset/ portion of the URLs with a custom scheme (e.g. file///android_asset/mypage.html becomes myscheme:///mypage.html), and place these custom scheme URLs in your page. Implement WebViewClient's shouldOverrideUrlLoading, check if the URL begins with the custom scheme and if so redirect to the local page using webview.loadUrl.
mWebView.setWebViewClient(new WebViewClient()
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
if (url != null && url.startsWith("myscheme://"))
{
String newUrl = url.replace("myscheme://", "file://android_asset/");
mWebView.loadUrl(newUrl);
return true;
}
return false;
}
}

Related

Webview Webpage not available ERR_FILE_NOT_FOUND

I have been working on an android application, using Kotlin. Recently, I have been trying to implement video calls using Javascript with the help of Webview in android, the problem is that when loading the activity in which the Webview is in, it does not show the HTML page.
As far as I know, everything is in place, and the code from what I've researched should be fine. I have already rebuilt the application and it still does not work.
That code box below shows the supposed correct way to load the page, but I only get a: "ERR_FILE_NOT_FOUND"
val filePath = "file:///android_asset/Content/call.html"
webView.loadUrl(filePath)
Then I tried to access the page using the following syntax for the filepath:
val filePath = "./src/main/assets/Content/call.html"
And it worked, it stopped showing the error, but this is a problem, because it doesn't work like that on physical devices.
There is also this error that shows in logcat:
E/AndroidProtocolHandler: Unable to open asset URL: file:///android_asset/Content/call.html
I have also tried moving the files outside of the Content folder, does not work.
This is perhaps a very simple mistake, but I can't find the solution. It would be highly appreciated if any of you could help me, thank you.
You should be using a WebViewAssetLoader. This is the android recommended way to load static webpages and you should not be using file path.
WebviewAssetLoader will host the files in the below path - http(s)://appassets.androidplatform.net/assets/...
In your case it would be - https://appassets.androidplatform.net/assets/Content/call.html
Sample code :
final WebViewAssetLoader assetLoader = new WebViewAssetLoader.Builder()
.addPathHandler("/assets/", new AssetsPathHandler(this))
.build();
webView.setWebViewClient(new WebViewClient() {
#Override
#RequiresApi(21)
public WebResourceResponse shouldInterceptRequest(WebView view,
WebResourceRequest request) {
return assetLoader.shouldInterceptRequest(request.getUrl());
}
#Override
#SuppressWarnings("deprecation") // for API < 21
public WebResourceResponse shouldInterceptRequest(WebView view,
WebResourceRequest request) {
return assetLoader.shouldInterceptRequest(Uri.parse(request));
}
});
WebSettings webViewSettings = webView.getSettings();
// Setting this off for security. Off by default for SDK versions >= 16.
webViewSettings.setAllowFileAccessFromFileURLs(false);
// Off by default, deprecated for SDK versions >= 30.
webViewSettings.setAllowUniversalAccessFromFileURLs(false);
// Keeping these off is less critical but still a good idea, especially if your app is not
// using file:// or content:// URLs.
webViewSettings.setAllowFileAccess(false);
webViewSettings.setAllowContentAccess(false);
// Assets are hosted under http(s)://appassets.androidplatform.net/assets/... .
// If the application's assets are in the "main/assets" folder this will read the file
// from "main/assets/www/index.html" and load it as if it were hosted on:
// https://appassets.androidplatform.net/assets/www/index.html
webview.loadUrl("https://appassets.androidplatform.net/assets/www/index.html");
I just solved my specific problem.
It turns out that my "assets" folder was not inside the "main" folder, but inside a different folder in "src". I just created a new assets folder inside "main" and placed the files there, now the HTML page loads properly. Had to go into project view.

webview loadUrl(contentprovider example

I found webview loadUrl() method works with many types:
finally I have seen this also
content:// URLs pointing to a ContentProvider that is publishing content
available for streaming
from https://www.journaldev.com/9333/android-webview-example-tutorial.
content:// URLs pointing to a ContentProvider that is publishing content
available for streaming
I have searched a lot to find an example about how webview load a webpage using "content:// " urls. I got one link,
http://web.archive.org/web/20101108043507/http://www.techjini.com/blog/2009/01/10/android-tip-1-contentprovider-accessing-local-file-system-from-webview-showing-image-in-webview-using-content/
but that's not complete.
I am a beginner, so i don't know about content provider. Please give me an example of loading a web page using loadUrl("content://package name/htmlfile.html") in webview.
eg: webview.loadUrl("content://package name/htmlfile.html")
Give me an example, it should be good if it explains about it or github code link.
The WebView is an example of Android's View class that allows you to display web pages as a part of your activity layout. All that a WebView does by default is display a web page.
Adding a WebView to your app
To add a WebView to your app, you can either include the <WebView> element in your activity layout or set the entire Activity window as a WebView in onCreate().
Load a WebView
To load a web page in the WebView in the WebView, use loadurl().
WebView webView = (WebView) findViewById(id);
webView.loadUrl("https://www.page.com") // incase you are loading page from
server.
//Loading page from an asset folder
To load a locally available HTML file, place the HTML file in your android/assets folder.Use the following code to load it into the webView
webView.loadUrl("file:///android_asset/www/termofservice.html);
Unless you have a specific user requirement, you should not load HTML pages using the content:// protocol instead use file:// for offline files and http:// for files available on a remote server.
Before all of this works however, your app needs to have access to the internet permissions, which you can obtain by decalring this on your manifest:
<manifest ...>
<uses-permissions android:name = "android.permission.INTERNET"/>
</manifest... >
On SDK 21 and above you will have to request this permission explicitly:
In your MainActivity.java:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[] Manifest.permission.INTERNET}, REQUEST_INTERNET_PERMISSION);
}
You can self-check for the permission before loading the webView incase user denied you the particular permission.
int permissionStatus = ActivityCompat.checkSelfPermission(application, Manifest.permission.INTERNET);
if (permissionStatus == PackageManager.PERMISSION_GRANTED)
return true;
else if (permissionStatus == PackageManager.PERMISSION_DENIED)
return false;
else
return false;

Is shouldOverrideUrlLoading called for links within the same domain?

I have a hybrid application where I have a WebView which is implementing the shouldOverrideUrlLoading method (both the deprecated and the newest version). This should take over control before loading any external links or certain links within my domain. Without going into specifics, the code looks roughtly like this:
private WebView mWebView;
mWebView.setWebViewClient(new myWebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.isExternal() || url.contains("#specialCase")) {
// Do actions
return true;
}
return super.shouldOverrideUrlLoading(view, url);
}
});
I have noticed that all external links work properly, however shouldOverrideUrlLoading is not being called at all when the link is within my domain, so there is no way for me to detect those cases where I want to take over control.
The android documentation states
Give the host application a chance to take over the control when a new
url is about to be loaded in the current WebView.
Does that new mean different domain? Is there anything I am missing or doing wrong? Any ideas on how to detect the user has clicked a link pointing to the same domain?
Thank you in advance.
Finally found the reason why shouldOverrideUrlLoading was never been called.
Apparently the method is only called when the actual loading is about to start. Our web application is a single-page application, hence even though the URL changes, no new page is loaded and shouldOverrideUrlLoading is not called.

Android WebView (4.4) Converts Custom URL

I have an app that heavily uses the Android WebView to display my custom HTML content. The latest Android update (4.4/Kit-Kat/SDK-19) featured a redesigned WebView.
One of my users with a Nexus 5 reported a problem where some links cause the app to crash. I ran in the 4.4 emulator and debug into my WebViewClient's shouldOverrideUrlLoading() method. On all previously tested Android versions (2.2-4.3) the url String passed into the method had my custom url with "/" characters in it. In 4.4 the exact same link now has "\" characters in their place.
This doesn't make any sense to me. I load the HTML exactly the same, so somehow the new WebView converted all my slashes into backslashes.
Why does the new WebView do this?
Changes in URL handling are a known issue. Please see the migration guide for more detail.
The behaviour in this particular case will depend on what your base URL's scheme is, from what you're describing I'm guessing your base URL's scheme is "http(s)://" in which case the Chromium WebView performs URL normalization.
You might want to consider using the URI class to handle the discrepancy between the Classic and Chromium WebViews in this case.
I did more debugging and discovered I actually have the question reversed. Turns out the older versions of WebView did conversions of the URL, not the new one.
I load HTML with a format similar to this into a WebView:
link
I use the double back slashes as delimiters and parse the data later when the link is clicked. In older versions of WebView it converted my double backslash characters into forward slashes. It had been so long since I was in that code, I forgot I adjusted my code to use forward slashes rather than the backslashes in the original HTML.
The new version of WebView leaves my custom URL intact, giving me the exact same string as my original HTML. So turns out the old WebView is the problem not the new one.
The new WebView applies additional restrictions when requesting resources and resolving links that use a custom URL scheme. For example, if you implement callbacks such as shouldOverrideUrlLoading() or shouldInterceptRequest(), then WebView invokes them only for valid URLs.
If you are using a custom URL scheme or a base URL and notice that your app is receiving fewer calls to these callbacks or failing to load resources on Android 4.4, ensure that the requests specify valid URLs that conform to RFC 3986.
For example, the new WebView may not call your shouldOverrideUrlLoading() method for links like this:
Show Profile
The result of the user clicking such a link can vary:
If you loaded the page by calling loadData() or loadDataWithBaseURL() with an invalid or null base URL, then you will not receive the shouldOverrideUrlLoading() callback for this type of link on the page.
Note: When you use loadDataWithBaseURL() and the base URL is invalid or set null, all links in the content you are loading must be absolute.
If you loaded the page by calling loadUrl() or provided a valid base URL with loadDataWithBaseURL(), then you will receive the shouldOverrideUrlLoading() callback for this type of link on the page, but the URL you receive will be absolute, relative to the current page. For example, the URL you receive will be "http://www.example.com/showProfile" instead of just "showProfile".
Instead of using a simple string in a link as shown above, you can use a custom scheme such as the following:
Show Profile
You can then handle this URL in your shouldOverrideUrlLoading() method like this:
// The URL scheme should be non-hierarchical (no trailing slashes)
private static final String APP_SCHEME = "example-app:";
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith(APP_SCHEME)) {
urlData = URLDecoder.decode(url.substring(APP_SCHEME.length()), "UTF-8");
respondToData(urlData);
return true;
}
return false;
}
If you can't alter the HTML then you may be able to use loadDataWithBaseURL() and set a base URL consisting of a custom scheme and a valid host, such as "example-app:///". For example:
webView.loadDataWithBaseURL("example-app://example.co.uk/", HTML_DATA,
null, "UTF-8", null);
The valid host name should conform to RFC 3986 and it's important to include the trailing slash at the end, otherwise, any requests from the loaded page may be dropped.
to avoid webview below 4.4 convert backslash to forward slash, I just escape my url, then in Java code, use URI.decode to get the real url.That works for me.

Android: web view inside activity doesn't run locally stored web app properly

I'm dealing with the following problem:
I have a webview inside an activity:
webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDatabaseEnabled(true);
webView.getSettings().setBuiltInZoomControls(false);
webView.getSettings().setSupportZoom(false);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.setScrollbarFadingEnabled(false);
webView.setWebViewClient(new HelloWebViewClient());
System.out.println("percorso" + path.toString());
url.setText(path.toString());
webView.loadUrl("local path of my html file" .....\..\..page.htm);
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
This is the app I'd like to run locally offline: http://miniapps.co.uk/checklist/
If I open the browser and i go to the link above everything works fine, so the android browser is actually able to run it.
Now I have downloaded this app and I stored it in a folder on my sd card: download/myapp
in the folder I have the html file: check_list.htm and a folder (check_list files) with the javascript files needed to run it.
But when i open it inside my webview, the page is correcty loaded but doesn't work...i can't click on buttons and other elements, i can't actually interact with it....
I thought it could be a problem related to my webview settings so i used astro file manager to surf into my sd card, i clicked on check_list.htm, i chose html viewer and i got the same issues....
So I downloaded opera mobile, and i did the same but this time I opened it with opera and now it's working correctly.
Do you know why? How can I fix the problem? Am I missing some settings on my webview or is the webview\android broser that hase some problems opening locally stored html pages and running linked javascript files?
EDIT
If I open the android browser and I write the path of the HTML file, it works. I have to do it manually cause if I click on the HTML file, android browser it's not an option......so in the end it works both on android browser and opera, and it doesn't with HTML viewer and my web view.....so I'm missing some settings cause the web view should have the same features of the android browser....shouldn't it?
If "doesn't run" mean it does not open new windows, that's because you need to specify the webview client.
From the Javadoc for WebView
By default, requests by the HTML to open new windows are ignored. This is true whether they be opened by JavaScript or by the target attribute on a link. You can customize your WebChromeClient to provide your own behaviour for opening multiple windows, and render them in whatever manner you want.
See https://stackoverflow.com/a/3847016/94363 or rtfm...
Please confirm before, your local file is inside asset/www/index.html directory.

Categories

Resources