-1

I load the camera into a framelayout and i want to take a screenshot. of the camera preview.

to take a screenshot:

public void onClick(View v) {
    View v1 = L1.getRootView();
    v1.setDrawingCacheEnabled(true);
    Bitmap bm = v1.getDrawingCache();
    BitmapDrawable bitmapDrawable = new BitmapDrawable(bm);
    image = (ImageView) findViewById(R.id.ImageView011);
    image.setBackgroundDrawable(bitmapDrawable);

    //Bitmap bitmap = takeScreenshot();
    saveBitmap(bm);
}

to save the screenshot:

public void saveBitmap(Bitmap bitmap) {
    File imagePath = new File(Environment.getExternalStorageDirectory() + "/screenshot.png");
    FileOutputStream fos;
    try {
        fos = new FileOutputStream(imagePath);
        bitmap.compress(android.graphics.Bitmap.CompressFormat.PNG, 100, fos);
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        Log.e("GREC", e.getMessage(), e);
    } catch (IOException e) {
        Log.e("GREC", e.getMessage(), e);
    }
}

when the image is saved everything is in the screenshot except the camera preview enter image description here

the camera loads inside a frame layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="mobapptut.com.camapp.bellowLollipop"
android:baselineAligned="false"
android:id="@+id/containerImg">


<FrameLayout
android:id="@+id/camera_preview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="false">


</FrameLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView android:id="@+id/ImageView01"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />

</LinearLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="300dp"
android:id="@+id/captured_image"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:contentDescription="desc" />


<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/plusBtnImg"
android:layout_gravity="left|bottom"
android:src="@mipmap/zoom_in2"
android:layout_alignParentBottom="true"
android:layout_toEndOf="@+id/imageView2"
android:background="#00ffffff" />


<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/minusBtnImg"
android:layout_gravity="left|bottom"
android:src="@mipmap/zoom_out2"
android:layout_above="@+id/plusBtnImg"
android:layout_toRightOf="@+id/imageView2"
android:background="#00ffffff" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go Back!"
android:id="@+id/backButton"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_gravity="right|bottom"
android:layout_alignParentRight="true" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save Picture"
android:id="@+id/button12"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"/>

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Take Screenshot"
android:id="@+id/Button01"
/>

<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Take Screenshot"
android:id="@+id/ImageView011"
/>
</RelativeLayout>

What am i doing wrong please? Thanks in advance :)

Asimina88
  • 107
  • 3
  • 15
  • Did you check [this](http://stackoverflow.com/questions/13565221/taking-screenshot-camera-viewlayout-view-android-augmented-reality)? – solosodium Jun 13 '16 at 15:49

1 Answers1

2

Just pass your desired view as a argument in the below method

    public Bitmap takeScreenShot(View view) {
    view.setDrawingCacheEnabled(true);
    view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
    view.buildDrawingCache();
    if (view.getDrawingCache() == null)
        return null;
    Bitmap snapshot = Bitmap.createBitmap(view.getDrawingCache());
    view.setDrawingCacheEnabled(false);
    view.destroyDrawingCache();
    return snapshot;
}

And save this bitmap as per your needs.

Smit.Satodia
  • 597
  • 4
  • 13