Android截取當前屏幕保存到外部設備上

要保存屏幕首先要獲取當前窗口信息,再以流的形式轉成圖片寫入到文件保存起來,文件的命名可以根據當前時間。

 

 

 

package com.example.capture2sd;  
  
import java.io.File;  
import java.io.FileOutputStream;  
import java.io.IOException;  
import java.util.Calendar;  
  
import android.app.Activity;  
import android.content.ContentResolver;  
import android.graphics.Bitmap;  
import android.graphics.Bitmap.Config;  
import android.graphics.Point;  
import android.os.Bundle;  
import android.os.Environment;  
import android.view.Display;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.view.WindowManager;  
import android.widget.Button;  
import android.widget.Toast;  
  
/** 
 * 1. 實現截取當前屏幕的功能。 
 * 2. 把截取的圖片保存到個部設備中的某個目錄文件夾下面。 
 *  
 * @author Administrator 
 *  
 */  
public class MainActivity extends Activity {  
  
    private Button btn = null;  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
        btn = (Button) findViewById(R.id.GetPic);  
        btn.setOnClickListener(new OnClickListener() {  
  
            @Override  
            public void onClick(View v) {  
                // TODO Auto-generated method stub  
                GetAndSaveCurrentImage();  
            }  
        });  
    }  
  
    /** 
     * 獲取和保存當前屏幕的截圖 
     */  
    private void GetAndSaveCurrentImage() {  
        // 1.構建Bitmap  
        WindowManager windowManager = getWindowManager();  
        Display display = windowManager.getDefaultDisplay();  
        Point pt = new Point();  
        display.getSize(pt);  
        Bitmap Bmp = Bitmap.createBitmap(pt.x, pt.y, Config.ARGB_8888);  
        // 2.獲取屏幕  
        View decorview = getWindow().getDecorView();  
        decorview.setDrawingCacheEnabled(true);  
        Bmp = decorview.getDrawingCache();  
        // 保存路徑  
        String SavePath = getSDCardPath() + File.separator + "ScreenImage";  
        FileOutputStream fos = null;  
        // 3.保存Bitmap  
        try {  
            // 創建目錄  
            File path = new File(SavePath);  
            // 文件名  
            String filepath = SavePath + File.separator + getCurTime() + ".png";  
            System.out.println(filepath);  
            File file = new File(filepath);  
            // 目錄如果不存在就創建目錄  
            if (!path.exists()) {  
                path.mkdirs();  
            }  
            // 文件不存在就創建文件  
            if (!file.exists()) {  
                file.createNewFile();  
            }  
            fos = new FileOutputStream(file);  
            // 把獲取的圖片寫入文件  
            if (null != fos) {  
                Bmp.compress(Bitmap.CompressFormat.PNG, 90, fos);  
                fos.flush();  
                Toast.makeText(this, "截屏文件已保存至:" + path.toString(),  
                        Toast.LENGTH_LONG).show();  
            }  
  
        } catch (Exception e) {  
            System.out.println("----------Exception");  
            try {  
                if (fos != null)  
                    fos.close();  
            } catch (IOException e1) {  
                // TODO Auto-generated catch block  
                e1.printStackTrace();  
            }  
            e.printStackTrace();  
        }  
    }  
  
    /** 
     * 獲取當前時間,以當前時間來作為文件名保存 
     *  
     * @return 
     */  
    private String getCurTime() {  
        ContentResolver cv = this.getContentResolver();  
        String strTimeFormat = android.provider.Settings.System.getString(cv,  
                android.provider.Settings.System.TIME_12_24);  
        // 判斷是不是24小時制  
        if (strTimeFormat.equals("24")) {  
            System.out.println("---------24");  
        } else {  
            System.out.println("---------12");  
        }  
  
        Calendar c = Calendar.getInstance();  
        int year = c.get(Calendar.YEAR);  
        int month = c.get(Calendar.MONTH);  
        int day = c.get(Calendar.DAY_OF_MONTH);  
        int hour = c.get(Calendar.HOUR_OF_DAY);  
        int minute = c.get(Calendar.MINUTE);  
        int sec = c.get(Calendar.SECOND);  
        // 文件名  
        String strCurr = String.format("%04d%02d%02d_%02d%02d%02d", year,  
                month, day, hour, minute, sec);  
        return strCurr;  
    }  
  
    /** 
     * 獲取外部設備的目錄路徑功能 
     */  
    private String getSDCardPath() {  
        File sdcardDir = null;  
        // 判斷SDCard是否存在  
        boolean sdcardExist = Environment.getExternalStorageState().equals(  
                Environment.MEDIA_MOUNTED);  
        if (sdcardExist) {  
            sdcardDir = Environment.getExternalStorageDirectory();  
        }  
        return sdcardDir.toString();  
    }  
}  

 

 

發佈留言