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.
Asked
Active
Viewed 6,161 times
3

Abdul Rizwan
- 3,904
- 32
- 31
-
2Can we see your code and where exactly you're finding this issue? – Micho Jul 06 '15 at 05:55
-
Hello Micho...Below I have added the code to convert html code into image using webView and sharing this image using ShareIntent. – Abdul Rizwan Jul 14 '15 at 11:40
-
Does this answer your question? [Generate bitmap from HTML in Android](https://stackoverflow.com/questions/4633988/generate-bitmap-from-html-in-android) – Ismail Iqbal Nov 23 '20 at 10:59
3 Answers
9
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;
}

Gorio
- 1,606
- 2
- 19
- 32

Abdul Rizwan
- 3,904
- 32
- 31
-
-
This is exactly what I needed, however, I noticed that there is a loss of quality in the image generated from the webview, the text contained in it becomes a bit blurred, any idea why this could be happening? – FabioR Oct 20 '20 at 18:11
0
There can be many possible solutions. You can achieve it somehow by showing the full screen web view and programmatically capturing the screen shot of device and sending the data through a background service.
For capturing screenshot through program ; you can use following link:
http://amitandroid.blogspot.in/2013/02/android-taking-screen-shots-through-code.html

AnswerDroid
- 1,873
- 2
- 33
- 52
0
Check out this SO Answer https://stackoverflow.com/a/64853392/4944007. The library used in it really helped a lot in generating a bitmap from HTML

Ismail Iqbal
- 2,774
- 1
- 25
- 46