1

I'm trying to convert html files stored as assets to image so that I can share them. I tried the following code:

Picture picture = webView.capturePicture();
Bitmap b = Bitmap.createBitmap(
        picture.getWidth(), picture.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
picture.draw(c);

FileOutputStream fos = null;
try {
    fos = new FileOutputStream( Environment.getExternalStorageDirectory().toString() +"/temp.jpg");
    if ( fos != null ) {
        b.compress(Bitmap.CompressFormat.JPEG, 100, fos);
        Toast.makeText(SyllabusPage_NW.this, "created", Toast.LENGTH_SHORT).show();
        Intent sharingIntent = new Intent(Intent.ACTION_SEND);
        Uri screenshotUri = Uri.parse(Environment.getExternalStorageDirectory().toString() + "/temp.jpg");

        sharingIntent.setType("image/jpeg");
        sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
        startActivity(Intent.createChooser(sharingIntent, "Share image using"));
        fos.close();

    }
}
catch( Exception e ) {

    Toast.makeText(SyllabusPage_NW.this, "Error", Toast.LENGTH_SHORT).show();
}

The result of this is attached and the expected result is also attached. The webview captures only the first part of the html page. I wanna know how I can capture the entire html page instead of just the first bit?

Image that I'm getting

The Image I'm expecting

Evin1_
  • 12,292
  • 9
  • 45
  • 47
user3908652
  • 131
  • 1
  • 2
  • 8
  • http://stackoverflow.com/a/6592903/1061944 This will help you – Murtaza Khursheed Hussain Jan 26 '16 at 13:42
  • `trying to convert html files stored as assets to image`. What a terrible description. What has this to do with assets or with what the webview is displaying? You just want to have a picture of the complete actual page in the webview. Not only the visible part. – greenapps Jan 26 '16 at 13:47
  • The `capturePicture()` method named in the suggested link used to work. But not anymore with the new webkit if you run on 6.0 as far as i know. So for which Android version are you compiling? – greenapps Jan 26 '16 at 13:49
  • "I'm trying to convert html files stored as assets to image" -- um, why not just package the images? – CommonsWare Jan 26 '16 at 14:19
  • @MurtazaKhursheedHussain I tried the same thing but it doesn't work. – user3908652 Jan 26 '16 at 16:43
  • @greenapps Idk about the description, I chose those words because I wanted to be clear in saying that I'm using offline html files and the app works completely offline. and I've mentioned webView because that's the method I used previously. – user3908652 Jan 26 '16 at 16:43
  • @CommonsWare There are around 500+ html files. I'm just trying to implement a share button which on click would convert the html to image as in the second image for sharing. – user3908652 Jan 26 '16 at 16:43

1 Answers1

1

for API Lollipop and higher should enable slow draw:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    WebView.enableSlowWholeDocumentDraw();
}

then check the height of picture is correct by debugging.
if not measre webview first then layout.

webView.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
webView.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
webView.buildDrawingCache(true);

Bitmap b = Bitmap.createBitmap(webView.getDrawingCache());
webView.setDrawingCacheEnabled(false);
Mohammad
  • 69
  • 10