I have an app that generates a report in a TableLayout with a variable number of TableRows.
I use the following code to capture the Table into a bitmap:
TableLayout tl = (TableLayout)findViewById(R.id.CSRTableLayout);
Bitmap csr = Bitmap.createBitmap(tl.getWidth(), tl.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(csr);
tl.draw(canvas);
After I capture the screen as a bitmap, I attach it to an email using:
//folder already exists
File file = new File(folder.getAbsolutePath()+"/CSR"+csrnum+".jpg");
BufferedOutputStream bos = null;
FileOutputStream fos = null;
try {
fos=new FileOutputStream(file);
bos=new BufferedOutputStream(fos);
if(bos!=null) {
try {
csr.compress(Bitmap.CompressFormat.JPEG, 60, bos);
} catch (OutOfMemoryError e) {
Toast.makeText(CustomerReportActivity.this, "Out of Memory!", Toast.LENGTH_SHORT).show();
} finally {
fos.flush();
fos.close();
bos.flush();
bos.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
if(file.exists()) {
Intent i = new Intent(Intent.ACTION_SEND);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setType("message/rfc822");
String emailTo[] = {"***@****.***"};
i.putExtra(Intent.EXTRA_EMAIL,emailTo);
i.putExtra(Intent.EXTRA_SUBJECT,"...");
i.putExtra(Intent.EXTRA_TEXT, "...");
i.putExtra(Intent.EXTRA_STREAM,Uri.parse("file://"+file.getAbsolutePath()));
startActivity(i);
} else {
Toast.makeText(CustomerReportActivity.this, "Error attaching report to email!", Toast.LENGTH_SHORT).show();
}
The problem is that sometimes the table can get quite large, such as 1600x2400dp. I have gotten an OutOfMemoryError on the line "Bitmap.createBitmap(...)" Is there an alternative method to capture the screen while not overflowing the VM heap? If I understand correctly, calling Bitmap.createBitmap(...) is creating a bitmap the full size that is all just plain white. Is it possible to create it with NO pixel data and only fill it in once I called csr.compress(...) so that way it stays small? Any ideas/suggestions are greatly appreciated! Thanks in advance!