5

I'm taking screenshot as below:

 public static Bitmap takeScreenshot(Activity activity) {  
      View view = activity.getWindow().getDecorView();  
      view.setDrawingCacheEnabled(true);  
      view.buildDrawingCache();  
      Bitmap bitmap = view.getDrawingCache();  
      Rect rect = new Rect();  
      activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);  
      int statusBarHeight = rect.top;  
      int width = activity.getWindowManager().getDefaultDisplay().getWidth();  
      int height = activity.getWindowManager().getDefaultDisplay().getHeight();  
      Bitmap bitmap2 = Bitmap.createBitmap(bitmap, 0, statusBarHeight, width,  
          height - statusBarHeight);  
      view.destroyDrawingCache();  
      return bitmap2;  
    }  

But there is a Edittext in my layout. I click it and keyboard pop up, but screenshot doesn't contain keyboard using this way. How can I take screenshot programmatically also which can also capture the keyboard?

tshepang
  • 12,111
  • 21
  • 91
  • 136
lseeo
  • 799
  • 2
  • 9
  • 15
  • 3
    keyboard is not part your application layout. you are taking the screen shot of your layout(view). keyboard is not part of your layout. that's why you don't see it – Raghunandan May 25 '13 at 10:05
  • I know this. Any ideas on how to take screenshot for the whole phone screen? – lseeo May 25 '13 at 11:31
  • try the below answer but if you are testing on device you need to root the same. which is not right coz you can't ask users to root their device. You could try creating your own custom keyboard in your application. then use the above to take screen shot – Raghunandan May 25 '13 at 11:33
  • same issue is valid for videoview on screen. when screenshot is captured using above method, videoview on screen is not included in screenshot but a blank instead of it. I need a mechanism that simulate the affect that you press some device buttons(usually power and volume button simultaneously) and it screenshots everything on screen as they seemed(including keyboard, videoview etc.). Any ideas? – sulu.dev Apr 06 '15 at 14:34

2 Answers2

8

Read somewhere on the forums about another way: -

Process sh = Runtime.getRuntime().exec("su", null,null);
OutputStream  os = sh.getOutputStream();
os.write(("/system/bin/screencap -p " + "/sdcard/img.png").getBytes("ASCII"));
os.flush();

You can try that out. Don't know if it works or not, just saved it for a rainy day :P.

EDIT:

Found the link to the post - How to take a screenshots?

User says, screenshots work for him. I think this should suit your requirement.

Community
  • 1
  • 1
Sam
  • 3,413
  • 1
  • 18
  • 20
  • su is superuser does this need rooting on device? – Raghunandan May 25 '13 at 10:18
  • yes it does. :D. But i don't think there is another way to get the screenshot of the phone from an app. The most he can do is get the screenshot he is getting right now. – Sam May 25 '13 at 10:21
  • if he is testing on emulator i guess it already rooted. But it is not good to release your app asking users to root their device. Any how its a suggestion. I leave it to the OP to decide. Any way i got to learn something new from the link posted. – Raghunandan May 25 '13 at 10:23
3

It seems there is no way to get screenshot from screen of a device that is not rooted.Here,CommonsWare says:

If you mean "screenshot of somebody else's activity", that is not supported, for obvious privacy and security reasons.

Although you can get screenshot of emulator when your App is running!

Edit:

But there exists a library that is called ASL(Android Screenshot Library):

Android Screenshot Library (ASL) enables to programmatically capture screenshots from Android devices without requirement of having root access privileges. Instead, ASL utilizes a native service running in the background, started via the Android Debug Bridge (ADB) once per device boot.

I did not try it,but you can see more details in about it here.

Community
  • 1
  • 1
hasanghaforian
  • 13,858
  • 11
  • 76
  • 167