Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access local file in a WebView

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?

like image 607
dongshengcn Avatar asked May 16 '12 14:05

dongshengcn


People also ask

Does Localstorage work in WebView?

local storage work like json,so values store as "key:value" . you can add your browser unique id to it's key and using normal android localstorage.

How do I open an HTML file in WebView?

The WebView method to load our file takes a URI , so we need to access the HTML file using that URI . Since we stored it in the assets folder, we can access it using file:///android_asset/{file_name} . Now let's load that file in our MainActivity .

What can I use instead of WebView?

Alternatives to WebView If you want to send users to a mobile site, build a progressive web app (PWA). If you want to display third-party web content, send an intent to installed web browsers. If you want to avoid leaving your app to open the browser, or if you want to customize the browser's UI, use Custom Tabs.


1 Answers

Here are a couple of things to try:

  1. 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.

  2. 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" />

  3. 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;  
        }  
    }  
like image 59
Theo Avatar answered Sep 25 '22 13:09

Theo