Saving chart to bitmap [Android]


#1

Hi,

Im trying to save my ChartView to an image, however I do not get any of the LineSeries on it with the way I am trying currently, only the Axis and Legends.

private String takeScreenshot(){
		// image naming and path to include sd card appending name you choose for file
		String path = Environment.getExternalStorageDirectory().toString() + "/" + "shareChart.jpg";   

		// create bitmap screen capture
		Bitmap bitmap;
		ChartView v1 = (ChartView) mView.findViewById(R.id.chart_view);
		
		v1.setDrawingCacheEnabled(true);
		bitmap = Bitmap.createBitmap(v1.getDrawingCache());
		v1.setDrawingCacheEnabled(false);

		OutputStream fout = null;
		File imageFile = new File(path);

		try {
		    fout = new FileOutputStream(imageFile);
		    bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fout);
		    fout.flush();
		    fout.close();

		} catch (FileNotFoundException e) {
		    e.printStackTrace();
		    return null;
		} catch (IOException e) {
		    e.printStackTrace();
		    return null;
		}
		return path;
	}

The ChartView has multiple LineSeries on it, so all of them would need to be saved?  I assume I need to enable to drawing cache on the childern or LineSeries but cant figure out how…

Any help is appreciated!


#2

Hi AEB,

We don’t currently have any built-in feature for taking screen shots. The approach you have taken captures the parts of the chart that are drawn via the Android API, but not the plot area itself which is rendered using OpenGL. 

I’d suggest you take a look at https://code.google.com/p/android-screenshot-library/wiki/Usage as a starting point. We haven’t used this, but it looks like it might be promising.

Regards,

Robin Sillem


#3

That sadly requires including extra NDK items, which I’d like to avoid.  The more promising idea is to get access to the GLSurfaceView.Rendered (http://developer.android.com/reference/android/opengl/GLSurfaceView.Renderer.html), … is there any way I can get access to this via extension of one of the existing classes?