activity view 截图

来源:互联网 发布:哪个软件播放rmvb 编辑:程序博客网 时间:2024/06/01 17:14

核心

view.setDrawingCacheEnabled(true);view.buildDrawingCache();Bitmap bitmap = view.getDrawingCache();view.destroyDrawingCache(); 

sample from http://my.oschina.net/u/242041/blog/212755

public void screenShot(View view, String fileName) throws Exception {        view.setDrawingCacheEnabled(true);        view.buildDrawingCache();        //上面2行必须加入,如果不加如view.getDrawingCache()返回null        Bitmap bitmap = view.getDrawingCache();        FileOutputStream fos = null;        try {            //判断sd卡是否存在             boolean sdCardExist = Environment.getExternalStorageState()                     .equals(android.os.Environment.MEDIA_MOUNTED);             if(sdCardExist){                //获取sdcard的根目录                String sdPath = Environment.getExternalStorageDirectory().getPath();                //创建程序自己创建的文件夹                File tempFile= new File(sdPath+File.separator +fileName);                if(!tempFile.exists()){                    tempFile.mkdirs();                }                //创建图片文件                File file = new File(sdPath + File.separator+fileName+File.separator+ "screen" + ".png");                if(!file.exists()){                   file.createNewFile();                }                image.setImageBitmap(bitmap);                fos = new FileOutputStream(file);                if (fos != null) {                    bitmap.compress(Bitmap.CompressFormat.PNG, 90, fos);                    fos.close();                }            }        } catch (Exception e) {            Log.e(TAG, "cause for "+e.getMessage());        }// view.destroyDrawingCache(); 最后应该还要释放掉。 //    }
0 0
原创粉丝点击