当前位置:   article > 正文

Android 加载本地 Web 项目_android 加载web

android 加载web

1. web项目放在assert文件夹下

Android 端加载 html 方式

wv.loadUrl("file:///android_asset/xxx/xxx/index.html");
  • 1

Javascript 引用 assert 中的资源, 路径需要预先约定好

<link rel="xxx" href="/android_asset/xxx/xxx.js">
  • 1

2. web 项目放在 外置存储

Android端加载 html 方式

wv.loadUrl("file:///sdcard/Android/data/xxx/files/Download/xxx/index.html");
  • 1

Javascript 引用外置存储中的资源, 出于安全的考虑,禁止直接路径的调用,而需要 WebView 做资源拦截,替换为本地资源地址,然后以文件流的方式,回传给前端

		WebView wv = find....
        WebSettings settings = wv.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setAllowFileAccess(true);
        settings.setAllowFileAccessFromFileURLs(true);
        settings.setAllowContentAccess(true);
        settings.setDomStorageEnabled(true);

        wv.setWebViewClient(new MyWebViewClient());
        
 public class MyWebViewClient extends WebViewClient {
        @Nullable
        @Override
        public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
            String url = request.getUrl().toString();
            InputStream inputStream = null;
            String mimeType = "";
           
            if( url.startsWith("file:///assets")) {
                if(url.endsWith("js")) {
                    mimeType = "text/javascript";
                } else if(url.endsWith("css")) {
                    mimeType = "text/css";
                } else if(url.endsWith("json")) {
                    mimeType = "application/json";
                } else if(url.endsWith("bin")) {
                    mimeType = "application/octet-stream";
                }

                File externalFilesDir = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
                String loadPath = url.replace("file:///", externalFilesDir.getAbsolutePath() + File.separator +"dist"+ File.separator);
                File loadFile = new File(loadPath);
                try {
                    inputStream = new FileInputStream(loadFile);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if(inputStream != null) {
                Map<String, String> responseHeaders = new HashMap<>();
                responseHeaders.put("Connection", "close");
                responseHeaders.put("Content-Type", mimeType);
                responseHeaders.put("Date", "$dateString GMT");
                responseHeaders.put("Access-Control-Allow-Origin", "*");
                responseHeaders.put("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT, OPTIONS");
                responseHeaders.put("Access-Control-Max-Age", "600");
                responseHeaders.put("Access-Control-Allow-Headers", "accept, authorization, Content-Type");
                String encoding = "utf-8";
                int statusCode = 200;
                String reasonPhrase = "SUCCESS";
                return new WebResourceResponse(mimeType, encoding, statusCode, reasonPhrase, responseHeaders, inputStream);
            }

            return super.shouldInterceptRequest(view, request);
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/175205?site
推荐阅读
相关标签
  

闽ICP备14008679号