Taking screenshot of screen(programmatically)

2019-07-30 05:38发布

问题:

I would like to take a screenshot of an Android device programmatically.

I know this question has been asked many times.I am aware of two ways to do it.

  1. Using screenshot library(But I am getting black screen all the time).
  2. getting data from frame buffer with rooting from \dev\graphics\fb0(But it is always 0 byte and I do not know exact location where current screenshot of device is stored). My preferred requirement is getting better frame rate.My code is

    public void onCreate(Bundle savedInstanceState)
    {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
    
       Intent intent = new Intent();
       intent.setClass(this, ScreenshotService.class);
       bindService (intent,  new ServiceConnection() {
    
        @Override
        public void onServiceDisconnected(ComponentName name) 
        {
    
        }
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
          try {
                asl = IScreenshotProvider.Stub.asInterface(service);
          } catch (Exception ex) {
                Log.e("errrorrrrrrrrrrrrr","errrorrrrrrrrrrr", ex);
          }
        }
    }, Context.BIND_AUTO_CREATE);
    Button b1=(Button) this.findViewById(R.id.widget33);
    b1.setOnClickListener(new OnClickListener() {
    
        public void onClick(View view) 
        {
            try {
                asl.takeScreenshot();
            } catch (RemoteException ex) {
                Log.e("nnnnerrrrrrrrrrrrrr","errrrrrrrrrrrrr",ex);
            }
        }
    });
    }'
    

回答1:

Usually when you use screenshot, we get the screen images. Are you running it as said, also this could be a device thing, so I suggest you try using the same on other device.

Also some time the problem could be while writing the file back to sd. So Please provide with the full code that you tried.



回答2:

Dont post one question more than one time.

You can get the drawing cache of your view using the following code:

view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap b1 = view.getDrawingCache();