android 代码 截取屏幕,如何以编程方式在Android上截取屏幕截图?
這是允許我的屏幕截圖存儲在SD卡上的代碼,以后用于滿足您的任何需求:
首先,您需要添加適當的權限來保存文件:
這是代碼(在Activity中運行):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?DOM
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);}
如果要在片段視圖上使用它,請使用:View?v1?=?getActivity().getWindow().getDecorView().getRootView();
代替View?v1?=?getWindow().getDecorView().getRootView();
on?takeScreenshot()函數
注意:
如果對話框包含曲面視圖,則此解決方案不起作用。有關詳細信息,請檢查以下問題的答案:
總結
以上是生活随笔為你收集整理的android 代码 截取屏幕,如何以编程方式在Android上截取屏幕截图?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Go语言channel与select原理
- 下一篇: android sina oauth2.