0

I implemented a screenshot function triggered by an item in my navigation drawer. It works fine, but I want to make an screenshot of the whole page I see without the navigation drawer at that moment.

I used the layout suggested in the google tutorial to implement the navigation drawer:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!--Main content view-->
    <FrameLayout

            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    <!--Navigation Drawer-->
    <ListView
            android:id="@+id/left_drawer"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp"
            android:background="@color/sw21Background"/>


</android.support.v4.widget.DrawerLayout>

My code starts in the overridden listener method:

/* The click listner for ListView in the navigation drawer */
private class DrawerItemClickListener implements ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        selectItem(position, view); // my code, e.g. takeScreenshot(view);
    }
}

     /**
     * Swap fragments in the main content view
     */
    private void selectItem(int position, View view) {
        Fragment fragment = new Fragment();

        if (position == 0) {
            ...
        } else if (position == 3) {
            //Feedback
            // TODO get proper view of current page not the navigation drawer
            Bitmap bitmap = this.takeScreenShot(view.getRootView());
            Uri uri = saveBitmap(bitmap);
            sendEmail(uri);
        } ...

        //Insert the fragment by replacing any existing fragment
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.content_frame, fragment)
                .addToBackStack(null)
                .commit();

        //Highlight the selected item, update the title, and close the drawer
        mDrawerList.setItemChecked(position, true);
        setTitle(navigationItemTitles[position]);
        mDrawerLayout.closeDrawer(mDrawerList);

    }

The current view is partially hidden by the navigation bar, so the following code also takes a shot of the bar:

private Bitmap takeScreenShot(View view) {
    Bitmap bitmap = Bitmap.createBitmap(view.getWidth(),
                    view.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    view.draw(canvas);

    return bitmap;
}

I tried to use the view nested in the FrameLayout (content), but it does not cover the action bar.

Journeycorner
  • 2,474
  • 3
  • 19
  • 43
  • http://stackoverflow.com/questions/5321344/android-animation-wait-until-finished This should be what you are looking for. – JavaYu Jan 07 '15 at 15:57

3 Answers3

0

Close NavigationDrawer before taking screenshot in your takeScreenShot function. mDrawerLayout.closeDrawer(Gravity.LEFT);

private Bitmap takeScreenShot(View view) {
    mDrawerLayout.closeDrawer(Gravity.LEFT); // Close Drawer Here
    Bitmap bitmap = Bitmap.createBitmap(view.getWidth(),
                    view.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    view.draw(canvas);

    return bitmap;
}
Ahmed Nawaz
  • 1,634
  • 3
  • 21
  • 51
0

I had a similar problem and fixed it like this:

        View screenshotView = findViewById(R.id.content_frame);
        Bitmap bitmap = this.takeScreenShot(screenshotView);
        Uri uri = saveBitmap(bitmap);
        sendEmail(uri);

R.id.content_frame is my mainframe where I inflate all Fragments. Hope it works for you too.

0

I believe the right answer for that is to close the drawer and create an event handler for DrawerClosed, and on the Handler, you take the picture.

like this:

                    drawer.CloseDrawer((int)GravityFlags.Start);

                    //take silent screenshot
                    View rootView = Window.DecorView.FindViewById(Resource.Id.MainContainer);
                    drawer.DrawerClosed += (sender, e) =>
                    {
                        Bitmap tmpPic = ShareHandler.TakeScreenShot(this); //TakeScreenShot(rootView); 
                        Java.IO.File imageSaved = ShareHandler.StoreScreenShot(tmpPic);
                        if (imageSaved != null)
                        {
                            ShareHandler.ShareImage(imageSaved, this,"com.whatsapp", "", "ScreenShot Taken from: " + "Wharever");
                        }
                    };    
Roy Doron
  • 585
  • 9
  • 23