当前位置:操作系统 > 安卓/Android >>

Android 截屏并写入SD卡中

----------截屏方法
[java] 
private Bitmap shot() {   
    View views = getWindow().getDecorView(); 
    views.buildDrawingCache(); 
 
    // 获取状态栏高度 
    Rect frames = new Rect(); 
    views.getWindowVisibleDisplayFrame(frames); 
    int statusBarHeights = frames.top; 
    Display display = getWindowManager().getDefaultDisplay(); 
    int widths = display.getWidth(); 
    int heights = display.getHeight(); 
    //第一种方式      
    views.layout(0, statusBarHeights,widths, heights - statusBarHeights); 
    views.setDrawingCacheEnabled(true);//允许当前窗口保存缓存信息 ,两种方式都需要加上 
    Bitmap bmp = Bitmap.createBitmap(views.getDrawingCache()); 
    //第二种方式      
    // 1、source 位图  2、X x坐标的第一个像素  3、Y y坐标的第一个像素  4、宽度的像素在每一行  5、高度的行数 
    //Bitmap bmp = Bitmap.createBitmap(views.getDrawingCache(), 0, statusBarHeights,widths, heights - statusBarHeights); 
    return bmp;   

------------保存到SD卡方法
[java] 
try { 
    String status = Environment.getExternalStorageState(); 
    // 判斷SD卡是否存在 
    if (status.equals(Environment.MEDIA_MOUNTED)) { 
        File destDir = new File("文件夹名"); 
         
        if (!destDir.exists()) { 
            // 创建文件夾 
            destDir.mkdirs(); 
        } 
        File file = new File("图片名"); 
        // 判断文件夾是否存在 
        if (file.exists()) { 
            String pic_path ="文件夹名" +"图片名"+".png"; 
            FileOutputStream out = new FileOutputStream(pic_path); 
            shot().compress(Bitmap.CompressFormat.PNG,100, out); 
            out.flush(); 
            out.close(); 
        } 
    } 
} catch (FileNotFoundException e) { 
    e.printStackTrace(); 
} catch (IOException e) { 
    e.printStackTrace(); 

 
--------把Bitmap转为Drawable 放进imageView中
[java]
//Bitmap-->Drawable   
              BitmapDrawable bd=new BitmapDrawable(shot());   
              imageView.setBackgroundDrawable(bd);   
              imageView.setImageBitmap(shot());  

 
 
 
 

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,