Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android screenshot of activity with actionbar

I use these lines to take screenshot of my activity:

View toppest = ((ViewGroup) ctx.getWindow().getDecorView().findViewById(android.R.id.content)).getChildAt(0);
toppest.setDrawingCacheEnabled(true);
Bitmap bmap = toppest.getDrawingCache();
Utils.saveBitmapOnSdcard(bmap);
toppest.setDrawingCacheEnabled(false);

Anyway, this screenshot not contain actionbar.

How I can make screenshot with actionBar ?

For information: I use Sherlock actionbar implementation with windowActionBarOverlay option to "true".

like image 375
Sergey Vakulenko Avatar asked Nov 02 '12 11:11

Sergey Vakulenko


People also ask

What is ActionBar in Android?

Android ActionBar is a menu bar that runs across the top of the activity screen in android. Android ActionBar can contain menu items which become visible when the user clicks the “menu” button. In general an ActionBar consists of the following four components: App Icon: App branding logo or icon will be displayed here.

What is the purpose of an ActionBar?

In Android applications, ActionBar is the element present at the top of the activity screen. It is a salient feature of a mobile application that has a consistent presence over all its activities. It provides a visual structure to the app and contains some of the frequently used elements for the users.


2 Answers

I found solution. Need to use ctx.getWindow().getDecorView() View to get full screen bitmap cache.

Also, there is small detail: screenshot contains empty space for status bar. We skip status bar height with help of Window.ID_ANDROID_CONTENT top margin. Finally, this give full screenhot of activity with same size as we saw it before on our telephone :

  View view = ctx.getWindow().getDecorView();
  view.setDrawingCacheEnabled(true);

  Bitmap bmap = view.getDrawingCache();
  Log.i(TAG,"bmap:" + b);


  int contentViewTop = ctx.getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop(); /* skip status bar in screenshot */
  Storage.shareBitmapInfo = Bitmap.createBitmap(bmap, 0, contentViewTop, bmap.getWidth(), bmap.getHeight() - contentViewTop, null, true);

  view.setDrawingCacheEnabled(false);
like image 101
Sergey Vakulenko Avatar answered Nov 02 '22 05:11

Sergey Vakulenko


The solution can be found here:

Height of statusbar?

Rect rectgle= new Rect();
Window window= getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
int StatusBarHeight= rectgle.top;
int contentViewTop= 
    window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
int TitleBarHeight= contentViewTop - StatusBarHeight;

   Log.i("*** Jorgesys :: ", "StatusBar Height= " + StatusBarHeight + " , TitleBar Height = " + TitleBarHeight);
like image 35
OldSchool4664 Avatar answered Nov 02 '22 07:11

OldSchool4664