(转)Android中截取当前屏幕图片
生活随笔
收集整理的這篇文章主要介紹了
(转)Android中截取当前屏幕图片
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
該篇文章是說明在Android手機(jī)或平板電腦中如何實(shí)現(xiàn)截取當(dāng)前屏幕的功能,并把截取的屏幕保存到SDCard中的某個目錄文件夾下面。實(shí)現(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"/>
轉(zhuǎn)載于:https://www.cnblogs.com/greywolf/p/3248146.html
總結(jié)
以上是生活随笔為你收集整理的(转)Android中截取当前屏幕图片的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 六、CPU优化(4)NUMA架构
- 下一篇: RHEL6入门系列之十三,阶段练习1