Quantcast
Channel: Android*
Viewing all articles
Browse latest Browse all 531

Android 截取系统当前屏幕

$
0
0
  1. private String savePath;            //截屏后图片保存路劲  
  2.   
  3.  /* (non-Javadoc) 
  4.     * @see app.ui.TitleActivity#onCreate(android.os.Bundle) 
  5.     */  
  6.    @Override  
  7.    protected void onCreate(Bundle savedInstanceState) {  
  8.        super.onCreate(savedInstanceState);  
  9.     setContentView(R.layout.activity_prize);  
  10.        savePath = getSDCardPath() + "/cut_photo";  
  11.    }  
  12.   
  13.   
  14.   
  15. /** 
  16.     * 获取和保存当前屏幕的截图 
  17.     */  
  18.    private void GetandSaveCurrentImage() {  
  19.        // 1.构建Bitmap  
  20.        WindowManager windowManager = getWindowManager();  
  21.        Display display = windowManager.getDefaultDisplay();  
  22.        int w = display.getWidth();  
  23.        int h = display.getHeight();  
  24.        Bitmap Bmp = Bitmap.createBitmap(w, h, Config.ARGB_8888);  
  25.        // 2.获取屏幕  
  26.        View decorview = this.getWindow().getDecorView();  
  27.        decorview.setDrawingCacheEnabled(true);  
  28.        Bmp = decorview.getDrawingCache();  
  29.        // 3.保存Bitmap  
  30.        try {  
  31.            File path = new File(savePath);  
  32.            // 文件  
  33.            String filepath = savePath + "/screen.png";  
  34.            File file = new File(filepath);  
  35.            if (!path.exists()) {  
  36.                path.mkdirs();  
  37.            }  
  38.            if (!file.exists()) {  
  39.                file.createNewFile();  
  40.            }  
  41.            FileOutputStream fos = null;  
  42.            fos = new FileOutputStream(file);  
  43.            if (null != fos) {  
  44.                Bmp.compress(Bitmap.CompressFormat.PNG, 90, fos);  
  45.                fos.flush();  
  46.                fos.close();  
  47.                //Toast.makeText(this, "截屏文件已保存至SDCard/AndyDemo/",Toast.LENGTH_LONG).show();  
  48.            }  
  49.   
  50.        } catch (Exception e) {  
  51.            e.printStackTrace();  
  52.        }  
  53.    }  
  54.   
  55.   
  56.    /** 
  57.     * 获取SDCard的目录路径功能 
  58.     */  
  59.    private String getSDCardPath() {  
  60.        File sdcardDir = null;  
  61.        // 判断SDCard是否存在  
  62.        boolean sdcardExist = Environment.getExternalStorageState().equals(  
  63.                android.os.Environment.MEDIA_MOUNTED);  
  64.        if (sdcardExist) {  
  65.            sdcardDir = Environment.getExternalStorageDirectory();  
  66.        }  
  67.        return sdcardDir.toString();  
  68.    }  
  • Android
  • Icon Image: 

  • android
  • Android*
  • Developers
  • Android*
  • Theme Zone: 

    Android

    Include in RSS: 

    1

    Viewing all articles
    Browse latest Browse all 531


    <script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>