简体   繁体   中英

Access android assets folder using javascript file

I am injecting a javascript file to the WebView and that javascript file needs to load more files from the app assets folder. i used to load the files from remote server, but now i need to load them locally. I am getting "not allowed to load local resource". is this even possible? i can't find any solution here or using google. example:

...
webView.loadUrl("javascript:(function() {" +
                    "var parent = document.getElementsByTagName('head').item(0);" +
                    "var script = document.createElement('script');" +
                    "script.type = 'text/javascript';" +
                    "script.innerHTML = window.atob('" + encoded + "');" +
                    "parent.appendChild(script)" +
                    "})()");

this injects a "script.js" file into the webview. inside the script.js file i want to inject css background image that is located inside the app assets folder. when im trying to access "file:///android_asset" i get the "not allowed to load local resource" error.

if you want load your local html page and resources to the web view you should use webView.loadDataWithBaseURL

  public void loadLocalHtmlToWebView() throws IOException {

        WebView mWebView = findViewById(R.id.my_webview);

        File publicDir = new File(getCacheDir(), "public");

        if (publicDir.exists() == false) {

            publicDir.mkdirs();

            String[] ls = getAssets().list("public");


            for (int i = 0; i < ls.length; i++) {

                InputStream inputStream = getAssets().open("public/" + ls[i]);

                File outFileLocation = new File(publicDir, ls[i]);

                outFileLocation.createNewFile();

                Log.e("AMIR", "Wrinting to : " + outFileLocation.getPath());

                FileOutputStream fileOutputStream = new FileOutputStream(outFileLocation);

                byte[] buffer = new byte[1024];

                while (inputStream.read(buffer) != -1) {

                    fileOutputStream.write(buffer);

                }

                fileOutputStream.flush();
                fileOutputStream.close();

                inputStream.close();


            }

        }


        String indexHtml="";

        BufferedReader bufferedReader=new BufferedReader(new FileReader(new File(publicDir,"index.html")));

        String ln="";

        while((ln=bufferedReader.readLine())!=null){

            indexHtml+=ln;

        }

        bufferedReader.close();

        Log.e("AMIR","Html : "+indexHtml);


        String baseUrl = "file://" + publicDir.getPath() + "/";

        mWebView.loadDataWithBaseURL(baseUrl, indexHtml, "text/html", "UTF-8", null);

    }

Assets folder :

资产文件夹

my index.html code :

<html>

<head>

<title>Hello</title>

<head>

<body>
Hello
<img src="./img.jpg"/>

<body>

</html>

and this is a good and well explained tutorial for webView :

http://tutorials.jenkov.com/android/android-web-apps-using-android-webview.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM