-4

ASL (android-screenshot-library) Have a working example?

OR

How do you show an example used (How to use)?

(sorry for my English)

mcxxx
  • 594
  • 1
  • 8
  • 21
  • Note: All the previous answers will not capture the dialogs, context menus and toasts, I have answered this question before, See my answer [here](http://stackoverflow.com/a/39924035/3311219) – Tarek360 Oct 07 '16 at 18:53

1 Answers1

4
private void getScreenShot() {
    Date now = new Date();
    android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);

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

        // create bitmap screen capture
        View v1 = getWindow().getDecorView().getRootView();
        v1.setDrawingCacheEnabled(true);
        Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
        v1.setDrawingCacheEnabled(false);

        File imageFile = new File(mPath);

        FileOutputStream outputStream = new FileOutputStream(imageFile);
        int quality = 100;
        bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
        outputStream.flush();
        outputStream.close();

        openScreenshot(imageFile);
    } catch (Throwable e) {
        // Several error may come out with file handling or OOM
        e.printStackTrace();
    }
}

To open the captured snap.

private void openScreenshot(File imageFile) {
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_VIEW);
    Uri uri = Uri.fromFile(imageFile);
    intent.setDataAndType(uri, "image/*");
    startActivity(intent);
}

You need

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Anoop M Maddasseri
  • 10,213
  • 3
  • 52
  • 73