I want to programmatically take a screenshot of my Android application

I want to programmatically take a screenshot of my Android application, which is an application video callingusing the openSIPS protocol. During a video call, I need to take screenshots. I already tried something, but it gives a screenshot, except for a fragment of a video camera.

Here is my attempt:

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();
    }

VideoView extends SurfaceView, in which its contents do not pass through the drawing cache, so receiving it will return only a black screen instead of capturing video manipulation. Any help would be appreciated.

+3
source share
2 answers

Ok, I got a great 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;
        }

onDrawFrame(GL10 gl) class Renderer, , . InCallActivity - , GlSurfaceView. . . :)

+3

-

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

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();

: ?

Android?

+2

All Articles