Java – a 0-byte file appears when capturing a screen through screensap

I had trouble capturing the screen of a non - rooted phone

I tried this command on my computer:

adb shell /system/bin/screencap -p /sdcard/out.png

It works

However, whenever I try to use this command in my application, it is as follows:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

            String[] str ={"/system/bin/screencap","-p","/sdcard/out.png"};

            try {  
                Process ps = Runtime.getRuntime().exec(str); 
                try { 
                    ps.waitFor(); 
                } catch (InterruptedException e) { 
                    e.printStackTrace(); 
                }  
            } catch (IOException e) { 
                Toast.makeText(this,""+e,Toast.LENGTH_LONG).show(); }


}

I always get out of 0 bytes png. I think there are no errors in the source code, but I can't understand why such errors occur

Please help me solve this problem

thank you.

Solution

Search logcat like this

If you receive this error, it indicates a permission problem

Even if you add read_ FRAME_ Buffer permissions, it still does not work properly Usually, this means that the application does not have permission to read the frame buffer, and must compile with the system certificate or use Su to obtain root permission

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>