Search code examples
androidscreenshot

Taking screenshot of a particular layout


I am trying to take screenshot of a particular layout but the problem is,the screenshot is coming with the actionbar and also has a blue patch above it although at time of taking screenshot I hide the action bar but still it persists but it is working fine for fab button.

I am mentioning the code below-

                    try {

                        actionBar.hide();
                        String mPath = Environment.getExternalStorageDirectory().toString() + "/" + "Hi" + ".jpg";
                        fabSpeedDial.setVisibility(View.INVISIBLE);
                        // create bitmap screen capture
                        View v1 = getWindow().getDecorView().getRootView();
                        v1.setDrawingCacheEnabled(true);
                        Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
                        v1.setDrawingCacheEnabled(false);

                        File imageFile = new File(mPath);

                        FileOutputStream outputStream = new FileOutputStream(imageFile);
                        int quality = 100;
                        bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
                        outputStream.flush();
                        outputStream.close();
                        fabSpeedDial.setVisibility(View.VISIBLE);
                         actionBar.show();
                         return true;


                    } catch (Throwable e) {

                        e.printStackTrace();
                    }

Xml code-

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.ragnar.useless.MainActivity"

android:background="#ff4343"
android:id="@+id/l1">

<ImageView
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:layout_width="180dp"
    android:id="@+id/img"
    android:layout_height="180dp"
    android:src="@drawable/panda"/>
<io.github.yavski.fabspeeddial.FabSpeedDial
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="bottom|end"
app:fabGravity="bottom_end"
app:fabMenu="@menu/main_menu"
android:id="@+id/fabspeed"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
app:miniFabDrawableTint="?attr/colorPrimaryDark"
app:miniFabTitleTextColor="?attr/colorPrimaryDark"
>
</io.github.yavski.fabspeeddial.FabSpeedDial>


</RelativeLayout>

What I want is the screenshot of Relativelayout without getting the actionbar and the above bluepatch. ANy suggestions will be appreciated and also I have seen a similar question on stackoverflow but couldn't understand its answers so please explain what you are providing .THank you in advance.


Solution

  • This is the problem:

    View v1 = getWindow().getDecorView().getRootView();
    

    You are taking the screenshot of the root view. You have to place an ID on your RelativeLayout, which is the root of your activity layout, like this:

    android:id="@+id/myview"
    

    And then replace the first line I mentioned with:

    View v1 = findViewById(R.id.myview);
    

    You will get the bitmap for the layout, without the actionbar and the above blue bar (which is the background of the status bar). Let me know if it works for you!