Take screenscreen on Android by API

3

I have an app and one of the functions would take printscreen from the screen.

How to do this?

    
asked by anonymous 21.03.2015 / 18:42

2 answers

3

I found this code in this answer in the OS :

//nome e local onde será gravado
String mPath = Environment.getExternalStorageDirectory().toString() + "/" + ACCUWX.IMAGE_APPEND;   

//cria a imagem
Bitmap bitmap;
View v1 = mCurrentUrlMask.getRootView();
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);

OutputStream fout = null;
imageFile = new File(mPath);

try {
    fout = new FileOutputStream(imageFile);
    bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fout);
    fout.flush();
    fout.close();

} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

Alternative using canvas as another answer in OS:

public Bitmap screenShot(View view) {
    Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    view.draw(canvas);
    return bitmap;
}
    
21.03.2015 / 18:48
2

Just to complement the bigown response.

It has a solution to take printscreen from the android screen, you can use this in a service, but you need root:

    try {
         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();
   } catch (IOException e) {
         e.printStackTrace();
   } catch (InterruptedException e) {
         e.printStackTrace();
   }
    
21.03.2015 / 20:01