Android Development: Using Image From Assets In A WebView's HTML

后端 未结 6 2027
星月不相逢
星月不相逢 2020-11-28 07:37

In my app I\'m making a basic HTML help document. I wanted my app\'s logo in the HTML img tag itself, but I don\'t know how I\'d reference to the logo which will be stored i

相关标签:
6条回答
  • 2020-11-28 07:57

    dump them into assets folder and just put in the html

    <img src="filename.png">
    

    simple as that

    0 讨论(0)
  • 2020-11-28 07:58

    Consider we have placed an image your_image.png in assets/imgs.

    In earlier versions of android you can directly access images like below.

    webView.loadData("<img src='file:///android_asset/imgs/your_image.png'/>",
            "text/html", "UTF-8");
    

    But can't access files directly in latest versions due to added security concerns. for the latest versions we need to use WebViewAssetLoader. refer the below code.

    Helper class to load local files including application's static assets and resources using http(s):// URLs inside a WebView class. Loading local files using web-like URLs instead of "file://" is desirable as it is compatible with the Same-Origin policy.

        final WebViewAssetLoader assetLoader = new WebViewAssetLoader.Builder()
                .addPathHandler("/assets/", new WebViewAssetLoader.AssetsPathHandler(this))
                .build();
    
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public WebResourceResponse shouldInterceptRequest(WebView view,
                                                              WebResourceRequest request) {
                return assetLoader.shouldInterceptRequest(request.getUrl());
            }
        });
        webView.loadData("<img src='https://appassets.androidplatform.net/assets/imgs/your_image.png'/>",
                "text/html", "UTF-8");
    

    for more information please refer android dev portal links https://developer.android.com/reference/androidx/webkit/WebViewAssetLoader https://developer.android.com/jetpack/androidx/releases/webkit?fireglass_rsn=true

    0 讨论(0)
  • 2020-11-28 08:02

    Store the images in assets folder:

    enter image description here

    Read the image in the html from assets with file:///android_asset/

    for example:

    String sHtmlTemplate = "<html><head></head><body><img src=\"file:///android_asset/img/mybadge.png\"></body></html>";
    

    load inside the WebView:

    webView.loadDataWithBaseURL(null, sHtmlTemplate, "text/html", "utf-8",null);
    
    0 讨论(0)
  • 2020-11-28 08:13

    Put your Logo into the assets directory eg: assets/logo.png

    Then load your html with:

    webView.loadDataWithBaseURL("file:///android_asset/", htmlData, "text/html", "utf-8", null);
    

    Reference your img like:

    <img src="logo.png">
    
    0 讨论(0)
  • 2020-11-28 08:14

    You can reference assets with this URL syntax:

    file:///android_asset/YourAssetFilename
    
    0 讨论(0)
  • 2020-11-28 08:16

    Put your Logo into the assets directory Example : assets/logo.png

    Then

    String imgData="<img src=home.png>";
    
    webView.loadDataWithBaseURL("file:///android_asset/", imgData, "text/html", "utf-8", null);
    
    0 讨论(0)
提交回复
热议问题