Android實現(xiàn)截屏并保存操作功能
該篇文章是說明在Android手機(jī)或平板電腦中如何實現(xiàn)截取當(dāng)前屏幕的功能,并把截取的屏幕保存到SDCard中的某個目錄文件夾下面。
實現(xiàn)的代碼如下:
/**
* 獲取和保存當(dāng)前屏幕的截圖
*/
private void GetandSaveCurrentImage()
{
//1.構(gòu)建Bitmap
WindowManager windowManager = getWindowManager();
Display display = windowManager.getDefaultDisplay();
int w = display.getWidth();
int h = display.getHeight();
Bitmap Bmp = Bitmap.createBitmap( w, h, Config.ARGB_8888 );
//2.獲取屏幕
View decorview = this.getWindow().getDecorView();
decorview.setDrawingCacheEnabled(true);
Bmp = decorview.getDrawingCache();
String SavePath = getSDCardPath()+"/AndyDemo/ScreenImage";
//3.保存Bitmap
try {
File path = new File(SavePath);
//文件
String filepath = SavePath + "/Screen_1.png";
File file = new File(filepath);
if(!path.exists()){
path.mkdirs();
}
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream fos = null;
fos = new FileOutputStream(file);
if (null != fos) {
Bmp.compress(Bitmap.CompressFormat.PNG, 90, fos);
fos.flush();
fos.close();
Toast.makeText(mContext, "截屏文件已保存至SDCard/AndyDemo/ScreenImage/下", Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 獲取SDCard的目錄路徑功能
* @return
*/
private String getSDCardPath(){
File sdcardDir = null;
//判斷SDCard是否存在
boolean sdcardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
if(sdcardExist){
sdcardDir = Environment.getExternalStorageDirectory();
}
return sdcardDir.toString();
}
由于要對SDCard進(jìn)行操作,所以別忘記了在manifest.xml文件中賦以對SDCard的讀寫權(quán)限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
希望本文所述對大家學(xué)習(xí)Android軟件編程有所幫助。
相關(guān)文章
Android調(diào)用系統(tǒng)時間格式顯示時間信息
這篇文章主要介紹了Android調(diào)用系統(tǒng)時間格式顯示時間信息的使用方法,代碼很簡單2014-01-01
Android 數(shù)據(jù)庫SQLite 寫入SD卡的方法
如果手機(jī)沒有root,數(shù)據(jù)庫文件是無法查看到的,不方便調(diào)試。最好的辦法是把數(shù)據(jù)庫寫進(jìn)SD卡。通過本文給大家介紹Android 數(shù)據(jù)庫SQLite 寫入SD卡的方法,需要的朋友參考下吧2016-04-04
使用Broadcast實現(xiàn)Android組件間的通信
這篇文章主要為大家詳細(xì)介紹了使用Broadcast實現(xiàn)Android組件間的通信,感興趣的小伙伴們可以參考一下2016-06-06
Android開發(fā)之全屏與非全屏的切換設(shè)置方法小結(jié)
這篇文章主要介紹了Android開發(fā)之全屏與非全屏的切換設(shè)置方法,結(jié)合實例形式分析了Android全屏切換靜態(tài)與動態(tài)兩種實現(xiàn)方法,需要的朋友可以參考下2017-08-08
Android開發(fā)之ListView的簡單用法及定制ListView界面操作示例
這篇文章主要介紹了Android開發(fā)之ListView的簡單用法及定制ListView界面操作,結(jié)合實例形式分析了Android ListView界面布局相關(guān)操作技巧,需要的朋友可以參考下2019-04-04
Android序列化實現(xiàn)接口Serializable與Parcelable詳解
我們使用 Intent 傳遞數(shù)據(jù)的時候,putExtra() 所支持的數(shù)據(jù)類型事有限的,當(dāng)需要傳遞自定義對象的時候就需要序列化。Serializable更簡單但是會把整個對象進(jìn)行序列化因此效率比Parcelable低一些2022-12-12

