I want to save a .png image from AndroidPlot

I wrote code that displays a line graph. This graph is built using Android Plot .. How can I save this graph as a .png image?

+4
source share
4 answers
xyPlot.setDrawingCacheEnabled(true); int width = xyPlot.getWidth(); int height = xyPlot.getHeight(); xyPlot.measure(width, height); Bitmap bmp = Bitmap.createBitmap(xyPlot.getDrawingCache()); xyPlot.setDrawingCacheEnabled(false); FileOutputStream fos = new FileOutputStream(fullFileName, true); bmp.compress(CompressFormat.JPEG, 100, fos); 
+5
source

You can get a drawing cache of any kind as a bitmap using:

 Bitmap bitmap = view.getDrawingCache(); 

Then you can just save the bitmap to a file using

 FileOutputStream fos = c.openFileOutput(filename, Context.MODE_PRIVATE); bitmap.compress(Bitmap.CompressFormat.PNG, 90, fos); fos.close(); 

This example will save a bitmap in local storage, available only to your application. For more information on saving files, check out the docs: http://developer.android.com/guide/topics/data/data-storage.html

+4
source

Before calling the Bitmap bitmap = view.getDrawingCache(); method Bitmap bitmap = view.getDrawingCache(); you need to call the view.setDrawingCacheEnabled(true) method.

Anyway, this does not work on all views, if your View extends SurfaceView returns a bitmap, it will be a black image. In this case, you need to use the draw method of your view (link to another post) .

PS: slayton, if I could write comments, I would comment on your post, but I did not have enough reputation

+1
source

I found a solution in png format:

 plot = (XYPlot) findViewById(R.id.pot); plot.layout(0, 0, 400, 200); XYSeries series = new SimpleXYSeries(Arrays.asList(array1),Arrays.asList(array2),"series"); LineAndPointFormatter seriesFormat = new LineAndPointFormatter(); seriesFormat.setPointLabelFormatter(new PointLabelFormatter()); plot.addSeries(series, seriesFormat); plot.setDrawingCacheEnabled(true); FileOutputStream fos = new FileOutputStream("/sdcard/DCIM/img.png", true); plot.getDrawingCache().compress(CompressFormat.PNG, 100, fos); fos.close(); plot.setDrawingCacheEnabled(false); 
0
source

All Articles