简体   繁体   中英

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).

So here comes the problem: how to 'transport' some html from a webview to another one? (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).

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:

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

where MyJavaInterface class contains a 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(...)"); calling a my custom java interface and get in myMethod the html i need to transport to a new webview (the String myHtml).

In this method ( public void myMethod(String myHtml) ) i use the instruction

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).

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:

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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