Thursday 19 March 2015

Programatically take a screenshot of view on Android

Copy, paste and modify the function according to need 
,this function create a Bitmap of current screen view 
 and save the .png file to sd card .

so add this permission in manifest to   
<uses-permission android:name="android.permission
               .WRITE_EXTERNAL_STORAGE"/> 
 
private void captureScreen() {
        View v = getWindow().getDecorView().getRootView();
        v.setDrawingCacheEnabled(true);
        Bitmap bmp = Bitmap.createBitmap(v.getDrawingCache());
        v.setDrawingCacheEnabled(false);
        try {
            FileOutputStream fos = new FileOutputStream(
                   new File(Environment.getExternalStorageDirectory()
                  .toString(), "SCREEN" + System.currentTimeMillis() 
                                                 + ".png"));
            bmp.compress(CompressFormat.PNG, 100, fos);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

No comments:

Post a Comment