Search code examples
androidandroid-intentandroid-bitmap

Android:how to convert html code into image and send this image using ShareIntent?


I have used webView to display and capture the content as an image and send using share option.
But I need a to convert html code to image without using webview.
Please help me.
Thanks in advance.


Solution

  • I have done this using webView.

    webview = (WebView) findViewById(R.id.webView1);
    
    WebSettings settings = webview.getSettings();
    settings.setBuiltInZoomControls(true);
    settings.setUseWideViewPort(false);
    settings.setJavaScriptEnabled(true);
    settings.setSupportMultipleWindows(false);
    
    settings.setLoadsImagesAutomatically(true);
    settings.setLightTouchEnabled(true);
    settings.setDomStorageEnabled(true);
    settings.setLoadWithOverviewMode(true);
    
    WebView webview = (WebView) findViewById(R.id.webview);
    WebView.enableSlowWholeDocumentDraw();
    
    String RESULTDATA = "<html><body><h1>It's working</h1></body></html>";
    if (!RESULTDATA.equals(null)) {
        Log.e("info", RESULTDATA);
        webview.loadData(RESULTDATA, "text/html", null);
    }
    
    circleButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
    
            shareResultAsImage(webview);
        }
    });
    

    And shareResultAsImage(WebView) method need to define.

    private void shareResultAsImage(WebView webView) {
        Bitmap bitmap = getBitmapOfWebView(webView);
        String pathofBmp = MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "data", null);
        Uri bmpUri = Uri.parse(pathofBmp);
        final Intent emailIntent1 = new Intent(android.content.Intent.ACTION_SEND);
        emailIntent1.setType("image/png");
        emailIntent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        emailIntent1.putExtra(Intent.EXTRA_STREAM, bmpUri);
    
        startActivity(emailIntent1);
    }
    
    private Bitmap getBitmapOfWebView(final WebView webView) {
        Picture picture = webView.capturePicture();
        Bitmap bitmap = Bitmap.createBitmap(picture.getWidth(), picture.getHeight(), Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(bitmap);
        canvas.drawColor(Color.WHITE);
        picture.draw(canvas);
        return bitmap;
    }