4

I have written a code which plots a Line Graph. This graph is plotted by using Android Plot.. How can i save this graph as .png image??

Nick
  • 8,181
  • 4
  • 38
  • 63
user955167
  • 41
  • 1
  • 4

4 Answers4

5
        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);
4

You can get the drawing cache of any View as a bitmap with:

Bitmap bitmap = view.getDrawingCache();

Then you can simply save the bitmap to a file with:

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

This example will save the bitmap to the local storage which is only accessible by your app. For more information about saving files check out the docs: http://developer.android.com/guide/topics/data/data-storage.html

slayton
  • 20,123
  • 10
  • 60
  • 89
1

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

Anyway it doesn't work on all Views, if your View extends SurfaceView the bitmap returned will be a black image. In that cases you have to use the method draw of your view (link to another post).

P.S.: slayton if I could write comments I would comment your post but I haven't got enought reputation

Community
  • 1
  • 1
David Jesse
  • 2,345
  • 1
  • 14
  • 10
0

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);
rm-vanda
  • 3,122
  • 3
  • 23
  • 34