简体   繁体   English

Android:从webview读取图像,将图像写入webview

[英]Android: read image from webview, write image to webview

I will resume some tips i have found on the net to solve my question. 我将继续介绍一些我在网上找到的解决问题的技巧。

I have a https connection with some cookies to manage, so i used the 'mythic' android WebView to get the informations i needed (a part of a html page). 我有一个与一些cookie进行管理的https连接,因此我使用了“神话” android WebView来获取我需要的信息(html页面的一部分)。

So here comes the problem: how to 'transport' some html from a webview to another one? 因此,问题来了:如何将某些html从Webview“传输”到另一个? (example: i have a webmail site designed for a desktop browser and i want to show in my application the content of a mail adapted to the screen of my device). (例如:我有一个为台式机浏览器设计的Webmail网站,我想在我的应用程序中显示适应于设备屏幕的邮件内容)。

Even if the solution is a little bit complicated i will try to explain a part of the solution. 即使解决方案有点复杂,我也会尝试解释解决方案的一部分。

I define a java-class interface to call from the webview in the 'onCreate' activity method: 我在“ onCreate”活动方法中定义了一个从Web视图调用的Java类接口:

WebView webview = (WebView) findViewById(R.id.webview);
(...)
webview.addJavascriptInterface(new MyJavaInterface(), "myJavaInterface");

where MyJavaInterface class contains a public void myMethod(String myHtml) 其中MyJavaInterface类包含一个public void myMethod(String myHtml)

Then i use javascript injection to get the html from my webview using the istruction webview.loadUrl("javascript:window.myJavaInterface.myMethod(...)"); 然后,我使用javascript注入使用istruction webview.loadUrl("javascript:window.myJavaInterface.myMethod(...)");从我的Webview获取html webview.loadUrl("javascript:window.myJavaInterface.myMethod(...)"); calling a my custom java interface and get in myMethod the html i need to transport to a new webview (the String myHtml). 调用我的自定义Java接口并获取myMethod中的html,我需要将其传输到新的webview(字符串myHtml)。

In this method ( public void myMethod(String myHtml) ) i use the instruction 在这种方法( public void myMethod(String myHtml) )中,我使用了以下指令

newwebview.loadData("<html><body>"+myHtml+"</body></html>", "text/html", "utf-8");

to get my goal. 达到我的目标。 But that's not all because my new webview will show the html i need except for the images (external css-stylesheets and javascript files too). 但这还不是全部,因为我的新Web视图将显示除图像(外部CSS样式表和javascript文件)之外的我需要的html。

So: how to 'transport' the images? 那么:如何“传输”图像?

Here comes the solution to my question: 这是我的问题的解决方案:

In the constructor of MyJavaInterface class i put the code to bypass the ssl certificate management: 在MyJavaInterface类的构造函数中,我将代码绕过ssl证书管理:

try{
   SSLContext sslContext = SSLContext.getInstance("TLS");
   sslContext.init(null, new TrustManager[] { new X509TrustManager() {
      @Override
      public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType)
         throws java.security.cert.CertificateException {;}
      @Override
      public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType)
         throws java.security.cert.CertificateException {;}

      public java.security.cert.X509Certificate[] getAcceptedIssuers() {
         return new java.security.cert.X509Certificate[] {};  }
   }}, null);
   HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
}catch (Exception e) {;}

in the public void myMethod(String myHtml) for each <img src= found i use the content of 'src' to call the following code: 在每个<img src=public void myMethod(String myHtml) ,我使用'src'的内容来调用以下代码:

private String getImg(String src) {
        CookieSyncManager cookieSyncManager = 
                CookieSyncManager.createInstance(view.getContext());
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.setAcceptCookie(true);
        cookieSyncManager.sync();
        InputStream in = null;
        ByteArrayOutputStream out = null;
        HttpsURLConnection con = null;
        try {
            String cookies = CookieManager.getInstance().getCookie(Constants.HOME_PAGE+src);
            URL url = new URL(Constants.HOME_PAGE+src);
            con = (HttpsURLConnection) url.openConnection();
            con.setRequestProperty("Cookie", cookies);
            con.setDoInput(true);
            con.connect();
            in = new BufferedInputStream(con.getInputStream());
            out = new ByteArrayOutputStream();
            int c;
            while ((c = in.read()) != -1)
                out.write(c);
            out.flush();
            out.close();
            return "data:image/jpeg;base64," + 
            Base64.encodeToString(out.toByteArray(), Base64.DEFAULT);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            try{con.disconnect();} catch (Exception e) {;}
            try{in.close();} catch (Exception e) {;}
            try{out.close();} catch (Exception e) {;}       
        }
    }

so i replace the content of src with the one returned by getImg function. 所以我用getImg函数返回的内容替换了src的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM