How to Take a Screenshot of Other App Programmatically Without Root Permission, Like Screenshot Ux Trial

How to capture a screenshot of other Android applications from an app without root?

Have a look at Android-screenshot-library, enables to programmatically capture screenshots from Android devices without requirement of having root access privileges.

How to take snapshot of screen not only app in Android with code

To take screen shot of the device screen, Only if you have root
call the screencap binary like:

Process sh = Runtime.getRuntime().exec("su", null,null);
OutputStream os = sh.getOutputStream();
os.write(("/system/bin/screencap -p " + Environment.getExternalStorageDirectory()+ "/img.png").getBytes("ASCII"));
os.flush();
os.close();
sh.waitFor()

And to load that file into a bitmap,Use

public static Bitmap decodeSampledBitmapFromFile(String path,
int reqWidth, int reqHeight) {

// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);

// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(path, options);
}

public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;

if (height > reqHeight || width > reqWidth) {

final int halfHeight = height / 2;
final int halfWidth = width / 2;

// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}

return inSampleSize;
}

Android take screenshot from code, with root access

Well, seems I'm on my own with this one. So far this is what I've found, for those who have a similar problem. Note that this varies by version, and I am running 2.3 Gingerbread.

I found 2 files in /system/bin - screencap and screenshot.

screencap returns a bitmap in a format I don't know how to decode, while screenshot makes a regular bmp file called tmpshot.bmp at the root of the sdcard (Neither accept any kind of command parameters. They don't seem to accept -h,-help, --help etc either).

tmpshot.bmp seems a viable solution for now but I was looking to avoid writing and reading from sdcard as this is quite the bottleneck. I will keep searching, but for those who need a solution now, here you are.

EDIT: Upon further trials I found the screencap binary from another rom (MIUI to be specific) actually offers a few more options, where (miui)screencap -t png /sdcard/screen.png yields a valid png screenshot.



Related Topics



Leave a reply



Submit