简体   繁体   English

使用javascript文件访问android资产文件夹

[英]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. 我正在向WebView注入一个javascript文件,并且该javascript文件需要从应用资产文件夹中加载更多文件。 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. 我在这里或使用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. 这会将“ script.js”文件注入到webview中。 inside the script.js file i want to inject css background image that is located inside the app assets folder. 我要在script.js文件中注入位于应用资产文件夹内的css背景图像。 when im trying to access "file:///android_asset" i get the "not allowed to load local resource" error. 当我尝试访问“ file:/// android_asset”时,出现“不允许加载本地资源”错误。

if you want load your local html page and resources to the web view you should use webView.loadDataWithBaseURL 如果要将本地html页面和资源加载到Web视图,则应使用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 : 我的index.html代码:

<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 : 这是webView的一个很好而且很好解释的教程:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android webview,在assets文件夹中加载javascript文件 - Android webview, loading javascript file in assets folder Android WebView:使用脚本标签从我的资产文件夹加载外部javascript文件 - Android WebView: Using Script Tags to Load an external javascript file from my assets folder 从Internet访问时JavaScript工作正常,但从WebView Android上的文件夹资产访问页面时无响应 - JavaScript Working When Access From Internet But Not Responding When Access Page From Folder Assets On WebView Android 将 HTML 文件中的外部 javascript 文件从 android 资产文件夹加载到 WebView - Load external javascript file inside HTML file from android assets folder into WebView 如何从资产文件夹访问图像到javascript对象 - How to access image from assets folder to javascript object 将javascript移至素材资源文件夹 - moving javascript into assets folder Javascript-从资产文件夹中的文件实例化新文件类型 - Javascript - instantiate a new filetype from a file in the assets folder 有没有办法从Assets文件夹调用javascript文件到Shopify上的代码段? - Is there a way to call a javascript file from the assets folder, to a snippet on Shopify? 如何使用JavaScript从共享文件夹访问pdf文件 - How to access pdf file from a shared folder using javascript 使用javascript直接访问特定的驱动器,文件夹和文件 - direct access to a certain drive,folder and file using javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM