5

Is there a way to get an image of the current live wallpaper using the WallpaperManager API? I tried the following code, but it simply returned the the icon of the app that was used to set the wallpaper.

  PackageManager pm = getApplicationContext().getPackageManager();

    // Check if user has set a live wallpaper
    if (WallpaperManager.getInstance(this).getWallpaperInfo() != null) {
        Drawable wallpaperDrawable = WallpaperManager.getInstance(this).getWallpaperInfo().loadThumbnail(pm);
    }
TMS
  • 1,201
  • 1
  • 13
  • 20
  • WallpaperManager.getInstance(this).getDrawable() : only get static wallpaper not current live wallaper.You can get info of live wallaper by WallpaperManager.getInstance(this).getWallpaperInfo().But you cant get screenshot of live wallaper in you app.But Still there is one option availbale,its just optional way.You can get current live wallaper for 'Set Wallpaper' in your app and at that time you can get screenshot. – Kabir Aug 12 '19 at 18:09

1 Answers1

0

Have you tried to use MediaProjection API to achieve this. I suggest to look along the following code. For more information about the API refer this.

//You have to start the Screen Capture based on an action. 
//mWidth, mHeight depends on device screen width, height
mImageReader = ImageReader.newInstance(mWidth, mHeight, PixelFormat.RGBA_8888, 2);
mVirtualDisplay = sMediaProjection.createVirtualDisplay(SCREENCAP_NAME, mWidth, mHeight, mDensity, VIRTUAL_DISPLAY_FLAGS, mImageReader.getSurface(), null, mHandler);
mImageReader.setOnImageAvailableListener(new ImageAvailableListener(), mHandler)
startActivityForResult(mProjectionManager.createScreenCaptureIntent(), REQUEST_CODE);

//Then convert the media captured to an Image
sMediaProjection.stop();
image = mImageReader.acquireLatestImage();
Image.Plane[] planes = image.getPlanes();
ByteBuffer buffer = planes[0].getBuffer();
bitmap = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888);
bitmap.copyPixelsFromBuffer(buffer);
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "SNAPSHOT.jpg");
fos = new FileOutputStream(file);
bitmap.compress(CompressFormat.JPEG, 100, fos);
fos.close();
Anjana
  • 903
  • 1
  • 5
  • 13