0

I posted this earlier and it was flagged as a duplicate post. I then clarified my write-up to show that the suggested solution didn't apply to my problem and I was encouraged to resubmit my question. However, the duplicate flag on that post means that I'm being overlooked, so maybe someone will see this instead.

I'm trying to create an app that will take a screenshot of a selected area. Its a FrameLayout containing a cameraView. I am placing extra views on this layout - the best example I can give is like a pilot seeing an indtrument readout superimposed on top of his "normal" vision. I'm trying to take a screenshot of the cameraview, with the extra material superimposed on top.

I am "using" the cameraview as a textureview, not surface view (the suggested solution on the other post was for surfaceview)

However, when I do it, the "extra" views that had been placed on the camera view show up on the resultant saved image but the cameraview "background" itself is black even though I can see the camera's output on my app when I save the image. In other words, other than the background from the camera with foreground material, there is only the foreground material and a black background

Can anyone help?

My layout xml for this part of the code is:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/frlayout"
    android:layout_weight="0.82"
    android:layout_width="match_parent"
        android:layout_height="0dp">
            <TextureView
            android:id="@+id/cameraView"
                android:background="@android:color/transparent"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
             />
    </FrameLayout>

And the code that saves the image is:

    Bitmap bitmap;
    ViewGroup v1 = findViewById(R.id.frlayout);
    v1.setDrawingCacheEnabled(true);
    bitmap = Bitmap.createBitmap(v1.getDrawingCache());
    v1.setDrawingCacheEnabled(false);

    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = Environment.getExternalStorageDirectory()+ "/" + timeStamp + ".jpg";

    OutputStream fout = null;
    File imageFile = new File(imageFileName);

    try {
        fout = new FileOutputStream(imageFile);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 95, fout);
        fout.flush();
        fout.close();


    } catch (Exception e) {


    }
EventHorizon
  • 87
  • 1
  • 7
  • Try `getBitmap()` on the `TextureView`. See https://stackoverflow.com/a/27454417/115145 and https://stackoverflow.com/a/28096931/115145 – CommonsWare Jul 15 '19 at 22:15
  • Thanks - is there any way to get the contents of the FrameLayout, including whatever views have been dynamically appended, in one go, that is as a screengrab? The way its looking, I have to get all the individual views as bitmaps and then recomposite them myself. – EventHorizon Jul 15 '19 at 23:09
  • The reason I asked was because of this (https://stackoverflow.com/questions/26190852/android-taking-screenshot-of-the-selected-area-programmatically) - I thought it meant a screengrab of the specified area (given by its ID) might also include child views added to that ID - that is, anything within the boundaries of the ID's layout. – EventHorizon Jul 15 '19 at 23:24
  • Btw, the code used above did take a snapshot of "frlayout" but only the child view appeared, so it seems to be partially possible to take a composite snapshot in one go. Its just the camera view that doesn't co-operate – EventHorizon Jul 16 '19 at 09:43

0 Answers0