Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Take a screenshot - Background Service

I'm trying to take a screenshot using a background service. This service is like a Facebook chathead, but I want it to take an screenshot when I do a click.

Preview Pickture

I've developed some code but it doesn't work. The last I've tried was:

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

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

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

        OutputStream fout = null;
        File imageFile = new File(mPath);

        try {
            fout = new FileOutputStream(imageFile);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fout);
            fout.flush();
            fout.close();

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
}

But is taking an screenshot to my button not to the screen.

I know the problem is here:

View v1 = chatHead.getRootView();

But I don't know how to fix it. Can anyone help me?

I'm actually using Android Studio 2.2.2 and Android 4.0 or greater.

like image 552
Mister JJ Avatar asked May 31 '17 15:05

Mister JJ


People also ask

How do I bypass a screenshot due to security policy?

Launch “Chrome.” Then enter “ chrome://flags ” without quotes into the address bar. On the Chrome flags screen, type “ Incognito Screenshot ” without quotes into the search box. The “Incognito Screenshot” option now appears in the results.

How do I take a screenshot of a service?

Alt + Print Screen To take a quick screenshot of the active window, use the keyboard shortcut Alt + PrtScn. This will snap your currently active window and copy the screenshot to the clipboard.

How do I take a screenshot of a protected content?

Open the Restricted app where you want to take the screenshot. Click on the floating button on your screen, and select the Settings icon that looks like a toolbox. Enable the Screenshot option from the list and close the list. A second floating button will open on your screen — a camera icon of the left.


3 Answers

For Lollipop and above you can use the MediaProjection API of Google to take the screenshot but you need to ask for the permission from the user.

You can find the sample screen capture code using MediaProjection Here

For the devices less then Lollipop you need root permission for it.

like image 150
Sarthak Doshi Avatar answered Nov 09 '22 18:11

Sarthak Doshi


To get a screenshot containing views not belonging to your app you'll need to use the MediaProjectionManager.

See How to take a screen shot with status bar contents in android application?

like image 27
gjsalot Avatar answered Nov 09 '22 17:11

gjsalot


Use MediaProjectionManager#createScreenCaptureIntent given in the MediaProjection API of Android. What you are doing now is taking the chatHead's root view's information rather than information present on the complete screen of your device including the status bar at the top.

like image 45
ADITYA RANA Avatar answered Nov 09 '22 18:11

ADITYA RANA