Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> android截取屏幕圖片

android截取屏幕圖片

編輯:關於android開發

  1. package com.iceman.printscreen; 
  2.  
  3. import java.io.File; 
  4. import java.io.FileOutputStream; 
  5.  
  6. import android.app.Activity; 
  7. import android.graphics.Bitmap; 
  8. import android.graphics.Bitmap.Config; 
  9. import android.os.Bundle; 
  10. import android.os.Environment; 
  11. import android.view.Display; 
  12. import android.view.LayoutInflater; 
  13. import android.view.View; 
  14. import android.view.View.OnClickListener; 
  15. import android.view.WindowManager; 
  16. import android.widget.Button; 
  17. import android.widget.LinearLayout; 
  18. import android.widget.Toast; 
  19.  
  20. public class PrintScreenDemoActivity extends Activity { 
  21.     private Button mButton; 
  22.     private LinearLayout mLayout; 
  23.     private int mPrintNum; 
  24.     /** Called when the activity is first created. */ 
  25.     @Override 
  26.     public void onCreate(Bundle savedInstanceState) { 
  27.         super.onCreate(savedInstanceState); 
  28.         LayoutInflater inf = this.getLayoutInflater(); 
  29.         mLayout = (LinearLayout)inf.inflate(R.layout.main, null); 
  30.         setContentView(mLayout); 
  31.         mButton = (Button)findViewById(R.id.print_btn); 
  32.         mButton.setOnClickListener(new OnClickListener() { 
  33.             public void onClick(View v) { 
  34.                 GetandSaveCurrentImage(); 
  35.                 mPrintNum++; 
  36.                 mButton.setText("截屏次數:"+mPrintNum); 
  37.             } 
  38.         }); 
  39.     } 
  40.     private void GetandSaveCurrentImage()    
  41.     {    
  42.         //1.構建Bitmap    
  43.         WindowManager windowManager = getWindowManager();    
  44.         Display display = windowManager.getDefaultDisplay();    
  45.         int w = display.getWidth();    
  46.         int h = display.getHeight();    
  47.             
  48.         Bitmap Bmp = Bitmap.createBitmap( w, h, Config.ARGB_8888 );        
  49.             
  50.         //2.獲取屏幕    
  51.         View decorview = this.getWindow().getDecorView();   
  52.         decorview.setDrawingCacheEnabled(true);     
  53.         Bmp = decorview.getDrawingCache();     
  54.         String SavePath = getSDCardPath()+"/PrintScreenDemo/ScreenImage";  
  55.         
  56.         //3.保存Bitmap     
  57.         try {    
  58.             File path = new File(SavePath);    
  59.             //文件    
  60.             String filepath = SavePath + "/Screen_"+mPrintNum+".png";    
  61.             File file = new File(filepath);    
  62.             if(!path.exists()){    
  63.                 path.mkdirs();    
  64.             }    
  65.             if (!file.exists()) {    
  66.                 file.createNewFile();    
  67.             }    
  68.                 
  69.             FileOutputStream fos = null;    
  70.             fos = new FileOutputStream(file);    
  71.             if (null != fos) {    
  72.                 Bmp.compress(Bitmap.CompressFormat.PNG, 90, fos);    
  73.                 fos.flush();    
  74.                 fos.close();      
  75.                     
  76.                 Toast.makeText(this, "截屏文件已保存至SDCard/PrintScreenDemo/ScreenImage/下", Toast.LENGTH_LONG).show();    
  77.             }    
  78.         
  79.         } catch (Exception e) {    
  80.             e.printStackTrace();    
  81.         }    
  82.     }    
  83.       
  84.        /**  
  85.         * 獲取SDCard的目錄路徑功能  
  86.         * @return  
  87.         */  
  88.     private String getSDCardPath(){  
  89.         File sdcardDir = null;  
  90.         //判斷SDCard是否存在  
  91.         boolean sdcardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);  
  92.         if(sdcardExist){  
  93.             sdcardDir = Environment.getExternalStorageDirectory();  
  94.         }  
  95.          
  96.         return sdcardDir.toString();  
  97.     }  

 代碼如下:

 

Java代碼 

  1. package com.iceman.printscreen;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileOutputStream;  
  5.   
  6. import android.app.Activity;  
  7. import android.graphics.Bitmap;  
  8. import android.graphics.Bitmap.Config;  
  9. import android.os.Bundle;  
  10. import android.os.Environment;  
  11. import android.view.Display;  
  12. import android.view.LayoutInflater;  
  13. import android.view.View;  
  14. import android.view.View.OnClickListener;  
  15. import android.view.WindowManager;  
  16. import android.widget.Button;  
  17. import android.widget.LinearLayout;  
  18. import android.widget.Toast;  
  19.   
  20. public class PrintScreenDemoActivity extends Activity {  
  21.     private Button mButton;  
  22.     private LinearLayout mLayout;  
  23.     private int mPrintNum;  
  24.     /** Called when the activity is first created. */  
  25.     @Override  
  26.     public void onCreate(Bundle savedInstanceState) {  
  27.         super.onCreate(savedInstanceState);  
  28.         LayoutInflater inf = this.getLayoutInflater();  
  29.         mLayout = (LinearLayout)inf.inflate(R.layout.main, null);  
  30.         setContentView(mLayout);  
  31.         mButton = (Button)findViewById(R.id.print_btn);  
  32.         mButton.setOnClickListener(new OnClickListener() {  
  33.             public void onClick(View v) {  
  34.                 GetandSaveCurrentImage();  
  35.                 mPrintNum++;  
  36.                 mButton.setText("截屏次數:"+mPrintNum);  
  37.             }  
  38.         });  
  39.     }  
  40.     private void GetandSaveCurrentImage()     
  41.     {     
  42.         //1.構建Bitmap     
  43.         WindowManager windowManager = getWindowManager();     
  44.         Display display = windowManager.getDefaultDisplay();     
  45.         int w = display.getWidth();     
  46.         int h = display.getHeight();     
  47.              
  48.         Bitmap Bmp = Bitmap.createBitmap( w, h, Config.ARGB_8888 );         
  49.              
  50.         //2.獲取屏幕     
  51.         View decorview = this.getWindow().getDecorView();    
  52.         decorview.setDrawingCacheEnabled(true);      
  53.         Bmp = decorview.getDrawingCache();      
  54.         String SavePath = getSDCardPath()+"/PrintScreenDemo/ScreenImage";   
  55.          
  56.         //3.保存Bitmap      
  57.         try {     
  58.             File path = new File(SavePath);     
  59.             //文件     
  60.             String filepath = SavePath + "/Screen_"+mPrintNum+".png";     
  61.             File file = new File(filepath);     
  62.             if(!path.exists()){     
  63.                 path.mkdirs();     
  64.             }     
  65.             if (!file.exists()) {     
  66.                 file.createNewFile();     
  67.             }     
  68.                  
  69.             FileOutputStream fos = null;     
  70.             fos = new FileOutputStream(file);     
  71.             if (null != fos) {     
  72.                 Bmp.compress(Bitmap.CompressFormat.PNG, 90, fos);     
  73.                 fos.flush();     
  74.                 fos.close();       
  75.                      
  76.                 Toast.makeText(this, "截屏文件已保存至SDCard/PrintScreenDemo/ScreenImage/下", Toast.LENGTH_LONG).show();     
  77.             }     
  78.          
  79.         } catch (Exception e) {     
  80.             e.printStackTrace();     
  81.         }     
  82.     }     
  83.        
  84.        /**  
  85.         * 獲取SDCard的目錄路徑功能  
  86.         * @return  
  87.         */   
  88.     private String getSDCardPath(){   
  89.         File sdcardDir = null;   
  90.         //判斷SDCard是否存在   
  91.         boolean sdcardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);   
  92.         if(sdcardExist){   
  93.             sdcardDir = Environment.getExternalStorageDirectory();   
  94.         }   
  95.           
  96.         return sdcardDir.toString();   
  97.     }   
  98. }  

 布局文件:

 

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:layout_width="fill_parent" 
  4.     android:layout_height="fill_parent" 
  5.     android:orientation="vertical" > 
  6.  
  7.     <Button 
  8.         android:id="@+id/print_btn" 
  9.         android:layout_width="fill_parent" 
  10.         android:layout_height="wrap_content" 
  11.         android:text="截屏" /> 
  12.  
  13. </LinearLayout> 

由於是把截屏的圖片存放到sd卡裡,所以要在manifest.xml文件中增加對sd卡的寫入權限.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved