如何获取顶层Activity的缩略图?ActivityManager.RunningTaskInfo.thumbnail返回null
You’re absolutely right that ActivityManager.RunningTaskInfo.thumbnail is effectively useless on modern Android versions—it’s been deprecated since API 21 and consistently returns null. Let’s walk through the most practical solutions based on your specific use case:
1. MediaProjection API (Official & Secure for All Apps)
This is the recommended, privacy-compliant way to capture screen content (including the top activity) on Android 5.0+. It requires explicit user consent, which is enforced to protect user privacy.
Here’s a simplified implementation outline:
- Trigger a screen capture permission request using
MediaProjectionManager.createScreenCaptureIntent() - Once permission is granted, initialize a
MediaProjectioninstance - Use
ImageReaderto capture screen frames, then convert those frames to aBitmap(your thumbnail)
Sample Kotlin code snippet:
private const val REQUEST_CODE_SCREEN_CAPTURE = 1001 // Launch permission request val mediaProjectionManager = getSystemService(MEDIA_PROJECTION_SERVICE) as MediaProjectionManager val captureIntent = mediaProjectionManager.createScreenCaptureIntent() startActivityForResult(captureIntent, REQUEST_CODE_SCREEN_CAPTURE) // Handle permission result override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { super.onActivityResult(requestCode, resultCode, data) if (requestCode == REQUEST_CODE_SCREEN_CAPTURE && resultCode == RESULT_OK) { data?.let { intentData -> val mediaProjection = mediaProjectionManager.getMediaProjection(resultCode, intentData) val screenWidth = resources.displayMetrics.widthPixels val screenHeight = resources.displayMetrics.heightPixels val imageReader = ImageReader.newInstance(screenWidth, screenHeight, ImageFormat.RGBA_8888, 1) val virtualDisplay = mediaProjection.createVirtualDisplay( "TopActivityCapture", screenWidth, screenHeight, resources.displayMetrics.densityDpi, DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR, imageReader.surface, null, null ) // Capture and process the screen image val image = imageReader.acquireLatestImage() image?.let { img -> // Convert Image to Bitmap (implement this helper function based on pixel buffers) val thumbnail = convertImageToBitmap(img) // Use the thumbnail as needed img.close() } // Clean up resources virtualDisplay.release() mediaProjection.stop() } } }
Note: You’ll need to implement the convertImageToBitmap() helper to translate the captured image’s pixel data into a usable Bitmap.
2. Accessibility Service (For Window Monitoring)
If your app’s core functionality requires tracking other apps’ windows, you can use an AccessibilityService to detect when the top activity changes. This requires the user to enable your app in the system’s Accessibility settings (a sensitive permission, so use it only if your app’s purpose justifies it).
Key steps:
- Create a custom
AccessibilityServiceand declare it in your manifest with theandroid.permission.BIND_ACCESSIBILITY_SERVICEpermission - Override
onAccessibilityEventto listen forTYPE_WINDOW_STATE_CHANGEDevents - Use the event data to get the top window’s bounds, then capture that area using the MediaProjection API (direct window capture is restricted on modern Android)
3. Capture Your Own App’s Activity Thumbnails
If you only need thumbnails for activities within your own app, you can directly capture the root view of the activity without any special permissions:
fun getActivityThumbnail(activity: Activity, targetWidth: Int, targetHeight: Int): Bitmap { val rootView = activity.window.decorView.rootView rootView.isDrawingCacheEnabled = true val fullSizeBitmap = rootView.drawingCache ?: return Bitmap.createBitmap(targetWidth, targetHeight, Bitmap.Config.ARGB_8888) val thumbnail = Bitmap.createScaledBitmap(fullSizeBitmap, targetWidth, targetHeight, true) rootView.isDrawingCacheEnabled = false return thumbnail }
This is lightweight and reliable for your app’s own UI, but won’t work for third-party apps.
Key Limitations to Remember
- Android’s privacy restrictions prevent capturing other apps’ content without explicit user consent (via MediaProjection or Accessibility permissions)
- There’s no way to silently get a thumbnail of another app’s activity—user interaction is mandatory
内容的提问来源于stack exchange,提问作者Ashutosh Sagar

