3

I want to programmatically take screen shot of my android application which is a video calling application by using openSIPS protocol. While on the video call, I need to take the screen shots. I have already tried something but it gives the screenshot except the videocall fragment.

Here is my try:

public static Bitmap takeScreenshot() {
        View rootView = mVideoView.getRootView();
        rootView.setDrawingCacheEnabled(true);
        //rootView.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.EXACTLY),
                //MeasureSpec.makeMeasureSpec(0, MeasureSpec.EXACTLY));
        // rootView.layout(0, 0, getMeasuredWidth(), getMeasuredHeight());
        rootView.buildDrawingCache(true);
        // rootView.destroyDrawingCache();
        return rootView.getDrawingCache();
    }

The videoView extends an SurfaceView, which has its content not go through the drawing cache, thus getting it will only returnes a black screen instead of a capture of the video layout. Any help will be appreciated.

Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
Alex Chengalan
  • 8,211
  • 4
  • 42
  • 56
  • 1
    Use the following URL http://stackoverflow.com/questions/15276346/how-to-take-screenshot-programmatically-and-save-it-on-gallery – Adel Feb 24 '14 at 09:36
  • 1
    But its not work, because here I need to try with a videoview – Alex Chengalan Feb 24 '14 at 09:39
  • there are lots of option available: http://stackoverflow.com/questions/3582603/programmatic-screencapture-on-mobile-device – Yogendra Feb 24 '14 at 09:40
  • `SurfaceView` is a quite diffrent from normal `View`. and this may be helpful http://stackoverflow.com/questions/18289544/taking-screenshot-programmatically-doesnt-capture-the-contents-of-surfaceview – Gopal Gopi Feb 24 '14 at 09:46

2 Answers2

3

Ok now, I got the perfect answer for me. Here it is.

if (InCallActivity.capture) {
            int widthx = width;
            int heightx = height;
            int screenshotSize = widthx * heightx;
            ByteBuffer bb = ByteBuffer.allocateDirect(screenshotSize * 4);
            bb.order(ByteOrder.nativeOrder());
            gl.glReadPixels(0, 0, widthx, heightx, GL10.GL_RGBA,
                    GL10.GL_UNSIGNED_BYTE, bb);
            int pixelsBuffer[] = new int[screenshotSize];
            bb.asIntBuffer().get(pixelsBuffer);
            bb = null;
            Bitmap bitmap = Bitmap.createBitmap(widthx, heightx,
                    Bitmap.Config.RGB_565);
            bitmap.setPixels(pixelsBuffer, screenshotSize - widthx,
                    -widthx, 0, 0, widthx, heightx);
            pixelsBuffer = null;

            short sBuffer[] = new short[screenshotSize];
            ShortBuffer sb = ShortBuffer.wrap(sBuffer);
            bitmap.copyPixelsToBuffer(sb);

            // Making created bitmap (from OpenGL points) compatible with
            // Android bitmap
            for (int i = 0; i < screenshotSize; ++i) {
                short v = sBuffer[i];
                sBuffer[i] = (short) (((v & 0x1f) << 11) | (v & 0x7e0) | ((v & 0xf800) >> 11));
            }
            sb.rewind();
            bitmap.copyPixelsFromBuffer(sb);
            InCallActivity.captureBmp = bitmap.copy(
                    Bitmap.Config.ARGB_8888, false);
            InCallActivity.capture = false;
        }

I put this code inside onDrawFrame(GL10 gl) method of my class Renderer , and it seems working. Here InCallActivity is the class where I using the GlSurfaceView. I got this answer from this link. Thanks for supports. Happy coding :)

Community
  • 1
  • 1
Alex Chengalan
  • 8,211
  • 4
  • 42
  • 56
2

You can try something like that

Bitmap bitmap;
  View v1 = findViewById(R.id.rlid);// get ur root view id
  v1.setDrawingCacheEnabled(true); 
  bitmap = Bitmap.createBitmap(v1.getDrawingCache());
  v1.setDrawingCacheEnabled(false);

and then for saving

ByteArrayOutputStream bytes = new ByteArrayOutputStream();
  bitmap.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
  File f = new File(Environment.getExternalStorageDirectory()
                    + File.separator + "test.jpg")
  f.createNewFile();
  FileOutputStream fo = new FileOutputStream(f);
  fo.write(bytes.toByteArray()); 
  fo.close();

Source: how to take screenshot programmatically and save it on gallery?

or you can also check this link How to programmatically take a screenshot in Android?

Community
  • 1
  • 1
Hassaan Rabbani
  • 2,469
  • 5
  • 30
  • 55