【问题标题】:Get an image/screenshot of live wallpaper on Android?在 Android 上获取动态壁纸的图像/屏幕截图?
【发布时间】:2019-08-02 21:39:52
【问题描述】:

有没有办法使用WallpaperManager API 获取当前动态壁纸的图像?我尝试了以下代码,但它只是返回了用于设置壁纸的应用程序的图标。

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

【问题讨论】:

  • WallpaperManager.getInstance(this).getDrawable() :只获取静态壁纸而不是当前的动态壁纸。您可以通过 WallpaperManager.getInstance(this).getWallpaperInfo() 获取动态壁纸的信息。但你不能在您的应用程序中获取动态壁纸的屏幕截图。但仍然有一个选项可用,它只是可选方式。您可以在您的应用程序中获取“设置壁纸”的当前动态壁纸,届时您可以获得屏幕截图。

标签: android wallpaper live-wallpaper wallpapermanager


【解决方案1】:

您是否尝试过使用 MediaProjection API 来实现这一点。我建议查看以下代码。有关 API 的更多信息,请参阅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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多