Take printscreen on Android by API

Asked

Viewed 2,271 times

4

I have a app and one of the functions would be to printscreen screen.

How to do this?

2 answers

4


I found this code on this response 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 response in the 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;
}

I put in the Github for future reference.

  • In case this code takes a print and saves in SD Card, I think only the part of bitmap is enough. (:

  • 2

    Yes, it can do with the result whatever it wants, not necessarily record in SD.

  • I tested and saw the link you sent. I removed the ACCUWX.IMAGE_APPEND, I have no idea what it is but replace it with the name+extension of the img (it is a good one by the name of the file as date-time not to replace it if you take more than one screenshot!!). Tb had to replace mCurrentUrlMask.getRootView(); by getWindow(). getDecorView(). getRootView(); because it was not taking print and not showing error. Only that it has a however, it print only from Activity, does not take off the screen of Android completely

  • @Felipeavelar as you do to leave a comment word marked, as you did with bitmap?

  • @Felipe.rce will see if I find anything else, to highlight any text in the posts you use it among crases

  • @bigown, I managed to take printscreen from the entire screen by following the link you sent me, only that it needs root... So I guess there’s no root

Show 1 more comment

2

Only to complement Maniero’s response.

You have a solution to take print screen from the Android screen, you can use this in a service, but needs 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();
   }
  • When you posted the comment, I was going to bring in this which is what I thought you had used.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.