在本地保存图像和web视图显示 [英] Save image locally and display in webview

查看:317
本文介绍了在本地保存图像和web视图显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个简单的一块code麻烦。 我从网上下载图像,并在本地与保存:

I have trouble with this simple piece of code. I download an image from the web and save it locally with :

               File mFile = new File(context.getFilesDir(), "a.png");
               if(!mFile.exists()) {         mFile.createNewFile();                }
               FileOutputStream fos = new FileOutputStream(mFile);
               fos.write(baf.toByteArray());
               fos.flush();
               fos.close();

我尝试在ImageView的显示此图像,它的工作。 现在,我尝试为显示保存图像上的WebView。

I try to display this image on the ImageView and it's working. Now i try to display the save image on a WebView.

  String data = "<body>" +"<img src=\"a.png\"/></body>";
  webview.loadDataWithBaseURL(getActivity().getFilesDir().toString(),data , "text/html", "utf-8",null);

它不工作,web视图显示什么。我尝试用一​​个PNG我把自己在/资产的WebView和它的工作。

It's not working, the webview shows nothing. I try the webview with a png i put myself in /assets and it's working.

我想我的语法来指向字符串数据的文件是错误的,但我不知道。

I think my syntax to point to the file in the String data is wrong but i'm not sure.

任何帮助AP preciated。

Any help appreciated.

感谢。

亚历克斯。

推荐答案

好了,测试许多不同的事情后,我结束了这方面的工作code。

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();
    }

这篇关于在本地保存图像和web视图显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆