日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

android 代码 截取屏幕,如何以编程方式在Android上截取屏幕截图?

發布時間:2023/12/19 Android 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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上截取屏幕截图?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。