0

I want to get screenshot of screen in android, I got screen shot as a Bitmap with using PixelCopy api for Api>=26 but I can not take screenshot for Api<25, When I try get screen shot of video I got black Screen, so I am trying to use MediaProjection get screen shot of device for Api<25. When I running below code I am getting this errror.

W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Intent.clone()' on a null object reference
W/System.err:     at com.kocsistem.pixageoneandroid.network.SocketIO.lambda$null$32$SocketIO(SocketIO.java:1359)
        at com.kocsistem.pixageoneandroid.network.-$$Lambda$SocketIO$LtCYia8Cg7W_4Pmz24LtXeOmWgY.run(lambda)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
        at java.lang.Thread.run(Thread.java:761)

Code

                Bitmap image;
                MediaProjection mediaProjection = null;

            if (Build.VERSION.SDK_INT >= 26) {
                Window window = ((Activity) context).getWindow();
                image = Bitmap.createBitmap(780, 1280, Bitmap.Config.ARGB_8888);
                PixelCopy.OnPixelCopyFinishedListener onPixelCopyFinishedListener = copyResult -> System.out.println("Pixel copy finished");
                PixelCopy.request(window, image, onPixelCopyFinishedListener, mHandler);
                image = Utils.getResizedBitmap(image, 500);
            } else {

/* image = Utils.screenShot(activity.getWindow().getDecorView().getRootView()); image = Utils.getResizedBitmap(image, 500);*/

                final DisplayMetrics metrics = new DisplayMetrics();
                activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);

                final int width = metrics.widthPixels;
                final int height = metrics.heightPixels;
                final int densityDpi = metrics.densityDpi;
                final int MAX_IMAGES = 10;

                mediaProjectionManager = (MediaProjectionManager) activity.getSystemService(Context.MEDIA_PROJECTION_SERVICE);

                try {

                    mediaProjection = mediaProjectionManager.getMediaProjection(Activity.RESULT_OK, (Intent) screenshotPermission.clone());

                    mImageReader = ImageReader.newInstance(width, height, PixelFormat.RGBA_8888, MAX_IMAGES);

                    mediaProjection.createVirtualDisplay("ScreenCaptureTest",
                            width, height, densityDpi,
                            DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
                            mImageReader.getSurface(), null, null);
                }catch (Exception e){
                    e.printStackTrace();
                }

                Image img = mImageReader.acquireLatestImage();
                byte[] datamm = Utils.getDataFromImage(img);
                image = BitmapFactory.decodeByteArray(datamm, 0, datamm.length);
            }
Diego
  • 937
  • 8
  • 24
  • You never appear to assign a value to `mediaProjection`, so it is `null`, and therefore you crash with a `NullPointerException` when trying to call a method on it. – CommonsWare Jul 30 '20 at 12:14
  • thanks for your reply, I editted my code at above and I got new error – Diego Jul 30 '20 at 12:55

0 Answers0