-1

i am trying to take screenshots of graph plotted using Androidplots in android. how can i take screenshots of those graph potted using Androidplot programmatic?

Babith
  • 5
  • 11

1 Answers1

2

You can use a method like this one:

@SuppressLint("SimpleDateFormat")
protected final void shoot(final Context ctx, final View v, final String prefix)
{

    // Get the bitmap from the view
    v.setDrawingCacheEnabled(true);
    final Bitmap bmp = v.getDrawingCache();

    final SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd@HHmmss");
    final Calendar cal = Calendar.getInstance();

    // Set file properties
    fileJPG = prefix + "_" + sdf.format(cal.getTime());

    /*
    Create a path where we will place our picture in the user's public
    pictures directory. Note that you should be careful about what you
    place here, since the user often manages these files.
    For pictures and other media owned by the application, consider
    Context.getExternalMediaDir().
    */
    final File path =
        Environment.getExternalStoragePublicDirectory
        (
            //Environment.DIRECTORY_PICTURES
            //Environment.DIRECTORY_DCIM
            Environment.DIRECTORY_DCIM + "/SomePath/"
        );

    // Make sure the Pictures directory exists.
    if(!path.exists())
    {
        path.mkdirs();
    }

    final File file = new File(path, fileJPG + ".jpg");

    try
    {
        final FileOutputStream fos = new FileOutputStream(file);
        final BufferedOutputStream bos = new BufferedOutputStream(fos, 8192);

        bmp.compress(CompressFormat.JPEG, 85, bos);

        bos.flush();
        bos.close();

        fileJPG = file.getPath();
    }
    catch (final IOException e)
    {
        e.printStackTrace();
    }
}

Note that you can take a screenshot of YOUR APP ONLY, not what is behind it, even if it is translucent.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • thanks it works before plotting graph, but after plotting graph it gives null pointer exception and crashes pls help..... – Babith Oct 20 '14 at 11:30
  • I'm using it with aChartEngine. It saves the graph, after it's drawn: I first draw the graph at the fragment creation, then by long clicking I take the screenshot. – Phantômaxx Oct 20 '14 at 11:52