在本地保存图像和web视图显示视图、图像、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 Fi...

我有这个简单的一块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();
    }
阅读全文

相关推荐

最新文章