android 屏幕截图

来源:互联网 发布:电脑网络参数在哪里 编辑:程序博客网 时间:2024/05/16 02:41

获取当前屏幕:

View view = activity.getWindow().getDecorView(); 


public static Bitmap getViewBitmap(View v) {                v.clearFocus();        v.setPressed(false);        //能画缓存就返回false        boolean willNotCache = v.willNotCacheDrawing();        v.setWillNotCacheDrawing(false);         int color = v.getDrawingCacheBackgroundColor();        v.setDrawingCacheBackgroundColor(0);        if (color != 0) {            v.destroyDrawingCache();        }        v.buildDrawingCache();        Bitmap cacheBitmap = v.getDrawingCache();        if (cacheBitmap == null) {           // Log.e(TAG, "failed getViewBitmap(" + v + ")", new RuntimeException());            return null;        }        Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);        // Restore the view        v.destroyDrawingCache();        v.setWillNotCacheDrawing(willNotCache);        v.setDrawingCacheBackgroundColor(color);        return bitmap;    }  //保存到sdcard SDPATH = Environment.getExternalStorageDirectory()+"/dong.png";    // savePic(getViewBitmap(v), "sdcard/xx.png");    private static void savePic(Bitmap b,String strFileName){     FileOutputStream fos = null;     try {     fos = new FileOutputStream(strFileName);     if (null != fos)     {     b.compress(Bitmap.CompressFormat.PNG, 90, fos);     fos.flush();     fos.close();     }     } catch (FileNotFoundException e) {     e.printStackTrace();     } catch (IOException e) {     e.printStackTrace();     }     }


SD卡权限:

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


文章出处:http://www.cnblogs.com/pcstart/archive/2011/09/05/2167187.html


原创粉丝点击