gpt4 book ai didi

android - 如何获取android屏幕的Bitmap?

转载 作者:行者123 更新时间:2023-12-02 21:11:33 27 4
gpt4 key购买 nike

我想以位图形式捕获 Android 屏幕而不进行屏幕截图。请帮助我。

谢谢。

最佳答案

试试这个:

private void takeScreenshot() {
Date now = new Date();
android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);

try {
// image naming and path to include sd card appending name you choose for file
String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg";

// create bitmap screen capture
View v1 = getWindow().getDecorView().getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);

File imageFile = new File(mPath);

FileOutputStream outputStream = new FileOutputStream(imageFile);
int quality = 100;
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
outputStream.flush();
outputStream.close();

openScreenshot(imageFile);
} catch (Throwable e) {
// Several error may come out with file handling or OOM
e.printStackTrace();
}
}

您可以这样查看文件:

private void openScreenshot(File imageFile) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(imageFile);
intent.setDataAndType(uri, "image/*");
startActivity(intent);
}

添加此权限以保存您的文件:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

关于android - 如何获取android屏幕的Bitmap?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33162611/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com