当前位置:首页 > 编程技术 > 正文

android 如何实现截屏

android 如何实现截屏

在Android中实现截屏,你可以通过以下几种方式: 1. 使用系统的截屏APIAndroid 4.0(API 级别 14)及以上版本提供了系统的截屏API,可以通过以...

在Android中实现截屏,你可以通过以下几种方式:

1. 使用系统的截屏API

Android 4.0(API 级别 14)及以上版本提供了系统的截屏API,可以通过以下步骤实现:

1. 在你的Activity中,获取屏幕的截图:

```java

public void takeScreenshot() {

View decorView = getWindow().getDecorView();

decorView.setDrawingCacheEnabled(true);

decorView.buildDrawingCache();

Bitmap bitmap = decorView.getDrawingCache();

File screenshotFile = new File(Environment.getExternalStorageDirectory() + "/screenshot.png");

try {

FileOutputStream out = new FileOutputStream(screenshotFile);

bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);

out.flush();

out.close();

最新文章