Ayodele Kayode
Ayodele Kayode

Reputation: 314

Taking screenshot in a Background Service

I am trying to make a bubble app that takes screenshot of other apps.

I found this project link with a media projection sample but the images are not been saved to the device.

is there a way i can save this image to the device or is there any other way i can take a screenshot with my app in a background service.

Upvotes: 2

Views: 331

Answers (1)

Ric17101
Ric17101

Reputation: 1388

I have checked the link and I think there's a way to save the file taken from screenshot.

From ImageTransmogrifier class

@Override
  public void onImageAvailable(ImageReader reader) {
    final Image image=imageReader.acquireLatestImage();

    if (image!=null) {
      Image.Plane[] planes=image.getPlanes();
      ByteBuffer buffer=planes[0].getBuffer();
      int pixelStride=planes[0].getPixelStride();
      int rowStride=planes[0].getRowStride();
      int rowPadding=rowStride - pixelStride * width;
      int bitmapWidth=width + rowPadding / pixelStride;

      if (latestBitmap == null ||
          latestBitmap.getWidth() != bitmapWidth ||
          latestBitmap.getHeight() != height) {
        if (latestBitmap != null) {
          latestBitmap.recycle();
        }

        latestBitmap=Bitmap.createBitmap(bitmapWidth,
            height, Bitmap.Config.ARGB_8888);
      }

      latestBitmap.copyPixelsFromBuffer(buffer);
      image.close();

      ByteArrayOutputStream baos=new ByteArrayOutputStream();
      Bitmap cropped=Bitmap.createBitmap(latestBitmap, 0, 0,
        width, height);

      cropped.compress(Bitmap.CompressFormat.PNG, 100, baos);

      byte[] newPng=baos.toByteArray();

      svc.processImage(newPng);
    }
  }

as you can notice there's a latestBitmap object there, from here you can actually save this bitmap to whatever place you want to save.

E.g. you can refer to this link https://www.simplifiedcoding.net/android-save-bitmap-to-gallery/ to save it on your Gallery :)

Upvotes: 2

Related Questions