I am trying to convert a ScrollView
to a Bitmap
, but the resulting image only contains part of the content.
This is how I create the image as a JPEG:
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 10, stream);
byte[] img = stream.toByteArray();
I tried to create the bitmap
object the following ways:
Try 1 :
int totalWidth=((ScrollView)contentView).getChildAt(0).getWidth();
int totalHeight=((ScrollView)contentView).getChildAt(0).getHeight();
Bitmap bitmap = Bitmap.createBitmap(totalWidth, totalHeight, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bitmap);
contentView.layout(contentView.getLeft(), contentView.getTop(), contentView.getRight(), contentView.getBottom());
contentView.draw(c);
Try 2:
Bitmap bitmap = Bitmap.createBitmap(contentView.getWidth(), contentView.getHeight(), Bitmap.Config.ARGB_8888);
contentView.layout(contentView.getLeft(), contentView.getTop(), contentView.getRight(), contentView.getBottom());
contentView.draw(c);
Try 3 :
Bitmap bitmap = Bitmap.createBitmap(contentView.getMeasuredWidth(), contentView.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
contentView.layout(contentView.getLeft(), contentView.getTop(), contentView.getRight(), contentView.getBottom());
contentView.draw(c);
Eventually, I add the image to a PDF like this:
public boolean saveReportAsPdf(Bitmap bitmap) {
try {
Image image;
String path = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/Reports";
File dir = new File(path);
if (!dir.exists())
dir.mkdirs();
Log.d("PDFCreator", "PDF Path: " + path);
File file = new File(dir, "audit.pdf");
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(file));
document.open();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 10, stream);
byte[] imageimg = Image.getInstance(stream.toByteArray());
image.setAlignment(Image.MIDDLE);
document.add(image);
document.close();
} catch (Exception i1) {
i1.printStackTrace();
}
return true;
}
This results in the following output:
This only shows the image up to row 7. As you can see below, there is also row 8 tot 13.
Row 8 to 13 are missing.