Android - How to take screenshot programmatically

android, java

Solution

If your phone is rooted try this

Process sh = Runtime.getRuntime().exec("su", null,null);

                    OutputStream  os = sh.getOutputStream();
                    os.write(("/system/bin/screencap -p " + "/sdcard/img.png").getBytes("ASCII"));
                    os.flush();

                    os.close();
                    sh.waitFor();

then read img.png as bitmap and convert it jpg as follows

Bitmap screen = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()+         
File.separator +"img.png");

//my code for saving
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    screen.compress(Bitmap.CompressFormat.JPEG, 15, bytes);

//you can create a new file name "test.jpg" in sdcard folder.

File f = new File(Environment.getExternalStorageDirectory()+ File.separator + "test.jpg");
            f.createNewFile();
//write the bytes in file
    FileOutputStream fo = new FileOutputStream(f);
    fo.write(bytes.toByteArray());
// remember close de FileOutput

    fo.close();

you have no access to the screen if your application is in background unless you are rooted, the code above can take the screenshot most effectively of any screen even if you are in background.

UPDATE

Google has a library with which you can take screenshot without rooting, I tried that, But iam sure that it will eat out the memory as soon as possible.

Try http://code.google.com/p/android-screenshot-library/

Problem

I need to screenshots of Android device or emulator programmatically when my application is installed and running in the background for every 200 milliseconds and save the images in my computer. I have implemented this procedure using below code and works only when my application is in foreground. I want to take screenshots when my application is in background as well. Below is my code: ``` public static Bitmap takeScreenshot(Activity activity, int ResourceID) { Random r = new Random(); int iterator=r.nextInt(); String mPath = Environment.getExternalStorageDirectory().toString() + "/screenshots/"; View v1 = activity.getWindow().getDecorView().findViewById(ResourceID); v1.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); v1.layout(0, 0, v1.getMeasuredWidth(), v1.getMeasuredHeight()); v1.setDrawingCacheEnabled(true); final Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache()); Bitmap resultBitmap = Bitmap.createScaledBitmap(bitmap, 640, 480, false); v1.setDrawingCacheEnabled(false); File imageFile = new File(mPath); imageFile.mkdirs(); imageFile = new File(imageFile+"/"+iterator+"_screenshot.png"); try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); resultBitmap.compress(CompressFormat.PNG, 100, bos); byte[] bitmapdata = bos.toByteArray(); //write the bytes in file FileOutputStream fos = new FileOutputStream(imageFile); fos.write(bitmapdata); fos.flush(); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return bitmap; } ``` How can I implement the functionality of Refresh and Save buttons of Screencapture in `Devices -> DDMS` programmatically? Can I achieve that?

Original source

Related problems