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;
Related
Suppose I want to create an android app it can load a website with webview. It also can load a local HTML page. I can create a hyperlink of a website from a local HTML page. Can I create a hyperlink from an online page to a local HTML page?
Just point your link using the right protocol
Offline
file:///
Online
http://
Example
<!-- This opens a page online -->
<a href='http://domian.com/test.html'>Test Online</a>
<!-- This opens a page offline in your assets directory-->
<!-- Make sure the file 'test.html' exists in your assets directory-->
<a href='file:///android_asset/test.html'>Test Offline</a>
The above offline access would work but only for version SDKs lesser than 24 (Marshmallow and below). Security grew stronger starting from Nougat to access offline html from anchor links. You can use the following strategy in your code to handle the protocol yourself.
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView webView, WebResourceRequest webResourceRequest) {
if (android.os.Build.VERSION.SDK_INT >= 24) {
if (webResourceRequest.getUrl().getScheme().equals("file")) {
//pass the actual target to webview
webView.loadUrl(webResourceRequest.getUrl().toString());
} else {
// If the URI is not pointing to a local file, open with an ACTION_VIEW Intent
webView.getContext().startActivity(new Intent(Intent.ACTION_VIEW, webResourceRequest.getUrl()));
}
}
else{
if (Uri.parse(url).getScheme().equals("file")) {
webView.loadUrl(url);
} else {
// If the URI is not pointing to a local file, open with an
//ACTION_VIEW Intent
webView.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
}
}
return true; // in both cases we handle the link manually
}
});
I am developing an Android application which uses WebView, and I would like to open a link in the app. The problem I have come across is that each of our customers use a different URL and is set via a preference in the android application. From, what I understand you have to set a link in the android manifest. Is this still achievable to have a link open in the application?
In order to open a link in the app just make sure to have an Activity that has a WebView on its layout. Then, in your activity do something like this:
WebView webview = (WebView) findViewById(R.id.your_web_view);
webview.loadUrl("http://your.url/");
Just read the user preference that stores the URL before calling loadUrl() and you're all set
You need to add a webview and give it an id. And then do this in your code:
//Identify the webview
WebView web_view = (WebView)findViewById(R.id.web_view);
//start web client
web_view.setWebViewClient(new WebViewClient());
//Load URL
web_view.loadUrl("your url");
And don't forget to add this line in your AndroidManifest.xml file :
<uses-permission android:name="android.permission.INTERNET" />
Hope it works!! :)
I need to save user login credentials for WebView in Android.
I have tried this, but with no luck.
WebView mWebview = (WebView) v.findViewById(R.id.wv_spielplan);
if(BuildConfig.VERSION_CODE < 21){
CookieManager.getInstance().setAcceptCookie(true);
} else {
CookieManager.getInstance().setAcceptThirdPartyCookies(mWebview, true);
}
CookieSyncManager.createInstance(getActivity());
CookieSyncManager.getInstance().startSync();
If the webview displays internet content, you may want to deal with the data from server side. Online or if you are showing "off line" assets content, i.e., content packaged with your app, allow javascript and DOM to work and you will be able to set a cookie inside webview. Edit: have a look at this: https://stackoverflow.com/a/5409155/5885018 and Set a cookie to a webView in Android
In my app, am loading a local html which is residing in SD card as
String extPath = getExternalFilesDir(null).toString();
String html = getHtml(extPath+File.separator+fileName); //it just reads html file and returns content as string
webvu.loadDataWithBaseURL("file://"+extPath+File.separator,html ,
"text/html","UTF-8", null);
the html file loaded in the web view (webvu) tries to load another html file with $.load ajax call
$.load("base.html",function(){ ... });
ajax load is throwing the below error. How can I resolve this
XMLHttpRequest cannot load
file:///storage/emulated/0/Android/data/com.example.sdcardwebview/files/sec.html.
Cannot make any requests from null. at null:1
I finally figured out the solution
The null origin issue happens only in JB, which supposedly has a webview based on new webkit which implements stricter same origin policy.
Hence the code in question works perfectly fine on all version of android below JB. To get the code work on JB, all we need to do is change web view settings. Just call
webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
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;
}
}