I dynamically built a relative layout of a chart of size 4022(width) x 3779(width). The chart however is only comprised of 50-100 Textviews so the result .png is small. This is the short code I use to capture the chart.
// chart_container was built dynamically before calling this function:
public void saveBitmap(RelativeLayout chart_container) {
int scale_ratio = 2;
int chartWidth = chart_container.getWidth();
int chartHeight = chart_container.getHeight();
Log.i(TAG, "orignal width:" + chartWidth + ", original height: " + chartHeight);
Bitmap mBitmap = Bitmap.createBitmap(chartWidth / scale_ratio, chartHeight / scale_ratio, Bitmap.Config.ARGB_8888); // crashes when scale_ratio is 1
Canvas mCanvas = new Canvas(mBitmap);
chart_container.draw(mCanvas);
out = new FileOutputStream("/data/data/package/chart.jpeg");
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
}
LogCat output:
orignal width:4022, original height: 3779
When the “scale_ratio” is 2, top left quarter of the chart is successfully saved. The file size is only 22k. When the “scale_ratio” is 1, however the App crashes due to out of memory at line “createBitmap”:
java.lang.OutOfMemoryError: bitmap size exceeds VM budget
Is there any good way to save the entire chart? Your help is greatly appreciated!!!
Update: I solved this problem by increasing the Emulator VM heap size 10 times. Now I have a complete chart.
I solved this problem by increasing the Emulator VM heap size 10 times. Now I have a complete chart.