Save image locally and display in webview

佐手、 提交于 2019-12-01 13:42:22

Ok, after testing lots of different things, i end up with this working code.

    WebView webview = (WebView) view.findViewById(R.id.imageView);      

    try {
        FileInputStream in = getActivity().openFileInput("image_behindfragment.png");
        BufferedInputStream buf = new BufferedInputStream(in);
        byte[] bitMapA= new byte[buf.available()];
        buf.read(bitMapA);
        Bitmap bM = BitmapFactory.decodeByteArray(bitMapA, 0, bitMapA.length);
        //imageview.setImageBitmap(bM);
        if (in != null) {
        in.close();
        }
        if (buf != null) {
        buf.close();

        String imgToString = Base64.encodeToString(bitMapA,Base64.DEFAULT);
        String imgTag = "<img src='data:image/png;base64," + imgToString               
                + "' align='left' bgcolor='ff0000'/>"; 
        webview.getSettings().setBuiltInZoomControls(true);

        webview.setInitialScale(30);
        WebSettings webSettings = webview.getSettings();
        webSettings.setUseWideViewPort(true);


        webview.loadData(imgTag, "text/html", "utf-8");

        }
    } catch (Exception e) {
        e.printStackTrace();
    }
String base = Environment.getExternalStorageDirectory().getAbsolutePath().toString();
String imagePath = "file://"+ base + "/a.png";
String html = "<html><head></head><body><img src=\""+ imagePath + "\"></body></html>";
WebView.loadDataWithBaseURL("file:///mnt/sdcard/Your Folder/", html, "text/html","utf-8", "");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!