-1

The area of camera is transparent.

I have search similar questions in stackoverflow,and found some answers,but all of these not suit my situation.

Hers is a question I found:How to programmatically take a screenshot in Android?

I have a custom view(CameraView) which can display what the camera grabs.But when I use the way above to screenshot,I found that the area CameraView is transparent,but this is what I do not want.

There is the picture I using phone to screenshot.For to display well I add black background to the ImageView's parent.

Before Screenshot:http://paradisehell.org/img/bug/before_screenshot.jpg

After Screenshot:http://paradisehell.org/img/bug/after_screenshot.jpg

Here is my code:

View view = ((Activity)mContext).getWindow().getDecorView().getRootView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
if (bitmap != null && listener != null){
    listener.onScreenShot(bitmap);
}

public void onScreenShot(Bitmap shot) {
    Log.e("TAG", CameraViewUtils.getBase64StringFromBitmap(shot));
    imageView.setImageBitmap(shot);
}

Do you have some ideas to solve it?Please tell me,there thanks.

Community
  • 1
  • 1
ChengTao
  • 87
  • 2
  • 9

1 Answers1

0

Put the Manifest permission first.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

And this is the code which helps you to take screenshots

private void Screenshot() {
    Date now = new Date();
    android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);


        String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg";

        View v1 = getWindow().getDecorView().getRootView();
        v1.setDrawingCacheEnabled(true);
        Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
        v1.setDrawingCacheEnabled(false);

        File image = new File(mPath);

        FileOutputStream output = new FileOutputStream(image);
        int quality = 100;
        bitmap.compress(Bitmap.CompressFormat.JPEG, quality, output);
        output.flush();
        output.close();

        openScreenshot(image);
    }

May this helps you.

Shubham Sejpal
  • 3,556
  • 2
  • 14
  • 31