Screenshot saved as a blank image [Android]

373 views Asked by At

I am trying to make a "camera" application that is nothing but a fullscreen viewfinder that will capture an image via screenshot when the user clicks anywhere on the screen. I know this will take a terrible picture, but I have a very specific use-case that requires it to be taken as a screenshot.

My problem is that the app is saving a completely blank image. I have requested and granted WRITE_EXTERNAL_STORAGE and CAMERA runtime permissions, so I'm thinking that my problem might be related to grabbing the wrong view.

Here is my activity_fullscreen.xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:id="@+id/container"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.cameraview.CameraView
        android:id="@+id/camera"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:keepScreenOn="true"
        app:autoFocus="true"
        app:aspectRatio="4:3"
        app:facing="back"
        app:flash="auto"/>

</FrameLayout>

Here are some of the methods used when the screenshot is taken:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fullscreen);
    mCameraView = findViewById(R.id.camera);
    if (mCameraView != null) {
        mCameraView.addCallback(mCallback);
    }

    mContentView = findViewById(R.id.camera);
    mContentView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            Bitmap bitmap = takeScreenshot();
            saveBitmap(bitmap);

        }

public Bitmap takeScreenshot() {
    View rootView = findViewById(android.R.id.content).getRootView();
    rootView.setDrawingCacheEnabled(true);
    return rootView.getDrawingCache();
}

public void saveBitmap(Bitmap bitmap) {
    File imagePath = new File(Environment.getExternalStorageDirectory() + "/screenshot.png");
    FileOutputStream fos;
    try {
        fos = new FileOutputStream(imagePath);
        bitmap.compress(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);
    }
}
0

There are 0 answers