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.
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.
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.
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.
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.
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.
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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With