Take a screenshot on Android

I am trying to create a custom launcher for Android, which should take a screenshot of the device immediately after the user clicks the home button and, therefore, right before my application (launcher) returns to the foreground. To a large extent, the idea is to take a screenshot of what the device was showing right before my launcher / action / app took up the screen.

I know that such a thing requires root access, but that is not a problem. Can someone show me how to take screenshots? Some code examples? Online tutorial? I looked online, but could not find anything useful. I'm trying to just take screenshots, like gazillion apps on the Android Market that already work with root permissions. Thanks in advance: -)

+4
source share
1 answer

This is a security breach. To do this, you must have a root device.

You can use this code with your own application: (I got this code from this answer to another question: fooobar.com/questions/18116 / ... )

// image naming and path to include sd card appending name you choose for file String mPath = Environment.getExternalStorageDirectory().toString() + "/" + ACCUWX.IMAGE_APPEND; // create bitmap screen capture 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) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } 

Try finding this code for more reference:

PS You can take a screenshot from Android using DDMS, but this is another story.

PPS If you want to transparently draw your launcher over the reality below it, you need to set the image format to RGBA (only with OpenGL) and process it accordingly. To do this, you do not need to take a screenshot.

Rooted device: use this code on the root device:

 process = Runtime.getRuntime().exec("su -c cat /dev/graphics/fb0"); InputStream is = process.getInputStream(); 
+4
source

Source: https://habr.com/ru/post/1415754/


All Articles