This article was launched on the public account “AndroidTraveler”.

background

Screenshots on Android phones are common in real-world applications.

Like powerpoint presentations, or tech blogs with illustrations.

That’s why it’s important to understand how Android phone screenshots work.

Use the screenshot tool that comes with AndroidStudio

On the left-hand toolbar of the Logcat TAB, there’s a photo icon that you can just click on.

After clicking, the following box will pop up, on which there are some operations to click, you can see that they are re-screenshot, rotate to left, rotate to right, copy to paste board.

Using Vysor

If you’re using Chrome, just add the Vysor extension.

To use Vysor, you need to install it on your phone as well, but if you start Vysor on your PC, it will automatically install it on your phone.

As you can see, you can take a screenshot by clicking the Take photo button.

Use the ADB command line

Execute the command

adb shell /system/bin/screencap -p /sdcard/tmp.png
Copy the code

The last string is the path, and you need to specify where to save your captured image.

In the demo above, it is /sdcard/tmp.png.

Then pull the screenshot saved on the phone to the desktop.

Execute the command

adb pull /sdcard/tmp.png .
Copy the code

The last string is the path to your desktop, and you need to specify where the image pulled by the phone will be saved on the client side.

In the demo above, is the current directory.

Use the system screenshot tool

This need not say more, directly use the system’s screenshot tool for screenshots. It is saved to the album by default.

You think this is where it ends?

Real background

With that background in mind, when I exhausted the screenshot methods above, the results for each method were as follows:

The first one: I messed up the screenshot tool in AndroidStudio and clicked on it.

Second: Vysor directly black screen.

Adb pulls out images with a size of 0 KB.

The fourth: do not take screenshots.

In fact, the first three methods didn’t work and we’re okay with that, but the last system method didn’t work. You have to think about whether you’re doing something at the system level.

Code Settings do not allow screenshots

In Android projects, you can set the interface to not allow screenshots.

Add the following statement to the Activity:

getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);
Copy the code

So, how to solve it?

Your APP

If it’s your APP, you can simply add a temporary comment to the code.

Other APP

You may think that since the Activity is set to prohibit the screen capture flag, so I open another application, and then set the Activity transparent, can play indirect screen capture operation?

The answer is no. The actual test verifies that the screen can not be taken as long as the screen is not visible, even if the onPause callback is used. Vysor has a black background superimposed on top of it.

So, if you really need to, just grab another phone and take a picture.

thinking

There must be some consideration (such as security) for not allowing screenshots in general, so it’s important to consider whether or not to force the screen capture.