Tutorials Android How to Programmatically Take a Screenshot in Android?

How to Programmatically Take a Screenshot in Android?

The following code snippet will help you to take a screenshot programmatically in Android. First you need to add the write file permission to save the captured screenshot.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Add the following Java code to capture the screenshot of running in an Activity.

private void captureScreenshot() {
    try {
        // image saving sd card path
        String mPath = Environment.getExternalStorageDirectory().toString() + "/" + System.currentTimeMillis() + ".jpg";

        // create bitmap screen capture
        View view = getWindow().getDecorView().getRootView();
        Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
        File imageFile = new File(mPath);
        FileOutputStream outputStream = new FileOutputStream(imageFile);
        int quality = 100;
        bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
    } catch (Throwable e) {
1 comment
Leave a Reply

Your email address will not be published.

Previous Post

How to Save Android Application’s Activity State

Next Post

How to Enable Deep Links for App Content in Android

Related Posts
By clicking “Allow All”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. Cookie Notice
Allow All