Skip to content Skip to sidebar Skip to footer

What Android Code Mimics The Screenshot Function (volume Down And Power Button Simultaneously)?

I am looking for a way to take screenshots of android device programmatically. I have looked at several solutions in stackoverflow that involve taking a screenshot of the running a

Solution 1:

If you are taking a screenshot of another Activity you will somehow have to get a reference to the View that encompasses the section of the screen you want to screenshot OR just capture the entire screen. If it is the entire screen you can get that with

// get the top level view from the activity in this contextViewtheView= getWindow().getDecorView();

Then to capture that View as a Bitmap you can use

// Take the Screen shot of a Viewpublicstatic Bitmap takeScreenshot(final View theView){
    theView.setDrawingCacheEnabled(true);
    return theView.getDrawingCache();
}    

// Release the drawing cache after you have captured the Bitmap// from takeScreenshot()publicstaticvoidclearScreenshotCache(final View theView){
    theView.setDrawingCacheEnabled(false);
}

From there you can save the Bitmap into a JPEG or PNG by calling .compress on the Bitmap.

As for running a task on a background thread there's enough documentation on several ways to do that like here.

Hope that gets you started!

Post a Comment for "What Android Code Mimics The Screenshot Function (volume Down And Power Button Simultaneously)?"