Galdo Monge
Galdo Monge

Reputation: 36

Problem with taking screenshots with the media projection android studio

  1. I have this code to make screenshots. In the virtual device of android studio this code works perfectly, but in my own device Samsung(with android 8.0) when I do the apk installer the program only take some screenshots. Example I press the button to take 10 screenshots, but the program only save me 2 and the rest doesn't exist. And now I don't have any idea what's the problem.

@SuppressLint("WrongConstant")

private void screenshot() {

    Point size = new Point();
    displayy.getSize(size);
    mWidth = size.x;
    mHeight = size.y;
    displayy.getMetrics(metrics);
    int mDensity = metrics.densityDpi;
    Handler handler=new Handler();


    mImageReader = ImageReader.newInstance(mWidth, mHeight, PixelFormat.RGBA_8888, 2);
    
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        displayyy = mMediaProjection.createVirtualDisplay(DISPLAY_NAME, mWidth, mHeight, mDensity, DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR, mImageReader.getSurface(), null, handler);
    }
    mImageReader.setOnImageAvailableListener(new ImageReader.OnImageAvailableListener() {
        int onImageCount = 0;
        
        @Override
        public void onImageAvailable(ImageReader reader) {

            Image image = null;
            FileOutputStream fos = null;
            Bitmap bitmap = null;

            try {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                    image = reader.acquireLatestImage();
                }
                if (image != null) {
                    Image.Plane[] planes = new Image.Plane[0];
                    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
                        planes = image.getPlanes();
                    }
                    ByteBuffer buffer = planes[0].getBuffer();
                    int pixelStride = planes[0].getPixelStride();
                    int rowStride = planes[0].getRowStride();
                    int rowPadding = rowStride - pixelStride * mWidth;

                    bitmap = Bitmap.createBitmap(mWidth + rowPadding / pixelStride, mHeight, Bitmap.Config.ARGB_8888);
                    
                    bitmap.copyPixelsFromBuffer(buffer);
                    
                    SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy_HH:mm:ss");
                    String formattedDate = df.format(Calendar.getInstance().getTime()).trim();
                    String finalDate = formattedDate.replace(":", "-");

                    String imgName = "/imagen"+ "_" + finalDate + ".jpg";
                    String dirPath = Environment.getExternalStorageDirectory().getAbsolutePath()
                            + "/example";
                    File dir = new File(dirPath);
                    if(!dir.exists()){
                        dir.mkdirs();
                    }

                    String mPath = dirPath + imgName;
                    File imageFile = new File(mPath);

                    fos = new FileOutputStream(imageFile);
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
                    Log.e(TAG, "captured image: " );
                }

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (fos != null) {
                    try {
                        fos.close();
                    } catch (IOException ioe) {
                        ioe.printStackTrace();
                    }
                }

                if (bitmap != null) {
                    bitmap.recycle();
                }

                if (image != null) {
                    image.close();
                }
            }
        }
    }, handler);
    
    mMediaProjection.stop();
    mMediaProjection=null;
}

Upvotes: 0

Views: 467

Answers (1)

Emarcia
Emarcia

Reputation: 11

I don't know if you've already solved your problem, but here's my solution: The method receives 4 parameters and the last one is the number of photos

enter code hereImageReader.newInstance(width: Int, height: Int, format: Int, maxImages: Int)

If you look good you are sending it in the last parameter the number of photos 2

enter code hereImageReader.newInstance(mWidth, mHeight, PixelFormat.RGBA_8888, 2);

enter link description here

Upvotes: 1

Related Questions