Android webview loadDataWithBaseURL how load images from assets?

前端 未结 4 1477
花落未央
花落未央 2020-12-03 05:41

In my project I have a files:

\"MyProject/assets/folder1/image1.jpg\"
\"MyProject/assets/folder1/index.html\".

In webView I need to open i

相关标签:
4条回答
  • 2020-12-03 06:20

    Have you give internet permission?

    <uses-permission android:name="android.permission.INTERNET" />
    
    0 讨论(0)
  • 2020-12-03 06:24

    I think you have to set the base to assets and add the sub folders to your image src's like this:

    webView.loadDataWithBaseURL("file:///android_asset/", readAssetFileAsString("folder1/index.html"), "text/html", "UTF-8", null);

    Html: <img src="folder1/image1.jpg">

    This worked for me on Android 5.1

    private String readAssetFileAsString(String sourceHtmlLocation)
    {
        InputStream is;
        try
        {
            is = getContext().getAssets().open(sourceHtmlLocation);
            int size = is.available();
    
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
    
            return new String(buffer, "UTF-8");
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
    
        return "";
    }
    
    0 讨论(0)
  • 2020-12-03 06:24

    try to like this

    try {
                String filePath = null;
                filePath = "Your File path";
                Bitmap bitmap = null;
    
                bitmap = BitmapFactory.decodeFile(filePath);
                Log.v("Image data-->", "" + bitmap);
                imageWidth = bitmap.getWidth();
                imageHeight = bitmap.getHeight();
                Log.e("Width", "" + imageWidth);
                filePath = "file://" + filePath;
                String html = "<html xmlns=\"http://www.w3.org/1999/xhtml\"><head><meta http-equiv=\"Content-Type\" content=\"text/html\";charset=utf-8\"/><title></title></head><body style=\"width:"
                        + imageWidth
                        + "px; height:"
                        + imageHeight
                        + "px; background:url("
                        + filePath
                        + ") no-repeat; position:relative;\">"
                        + getDivTag(mapList)
                        + "</body></html>";
    
                Log.v("MIS", "" + html);
                webview.getSettings().setSupportZoom(true);
                webview.loadDataWithBaseURL(null, html, "text/html", "utf-8", null);
    
                System.out.println(html);
    
            } catch (Exception e) {
                e.printStackTrace();
            }
    
    0 讨论(0)
  • 2020-12-03 06:38

    try like this

    WebView webview = (WebView)this.findViewById(R.id.webview);
    
    
    String html = "<html><head><title>TITLE!!!</title></head>";
    html += "<body><h1>Image?</h1><img src=\"icon.png\" /></body></html>";
    
    
    webview.loadDataWithBaseURL("file:///android_res/drawable/", html, "text/html", "UTF-8", null); 
    

    For more information try this link

    perfect LoadDataWithBaseurl

    0 讨论(0)
提交回复
热议问题