How to take screenshot programmatically on Android? [duplicate] - android

This question already has answers here:
How to programmatically take a screenshot on Android?
(26 answers)
Closed 8 years ago.
I want to take a fullscreen screenshot programmatically ,
for example, one of the android home screen or a menu.
How can I get a view of the home screen in my application?
I want to take it programmatically!!!
It doesn't matter if it requires root mode!
Help me please and sorry for my English!

Use following code
Bitmap bitmap;
View v1 = MyView.getRootView();
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
Here MyView is the View through which we need include in screen.

Follow the link
https://code.google.com/p/android-screenshot-library/downloads/list
it allows you entire screenshot of any screen not only your app
adb shell /system/bin/screencap -p /sdcard/img.png
this is a shell command take screenshot simple and fast
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();

In the adb shell you can use command as
adb shell screencap -p /sdcard/screen.png
adb pull /sdcard/screen.png
adb shell rm /sdcard/screen.png
I found this code on this guide on How to take a screenshot on Android.You can refer it for more details.Hope this helps.

Related

Take ScreenShot of Visible screen Android

I want to take screen shot of any visible screen from background. I don't want to be dependent on activity context. Whatever screen I open in my device like browser or any social app I just want to take screenshot of that.
I tried different approaches but it is depended on activity and we can only take screenshot of current App.
Any help would be appreciated. thanks
Works only for rooted diveces
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();

How to get the result of Runtime.getRuntime().exec directly

I'm working on a rooted android device. I'm trying to capture the screen and store the result in Bitmap for later usage.
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath();
path += "/img.png";
Process sh = Runtime.getRuntime().exec("su", null,null);
OutputStream os = sh.getOutputStream();
os.write(("/system/bin/screencap -p " + path).getBytes("ASCII"));
os.flush();
os.close();
sh.waitFor();
final Bitmap x = BitmapFactory.decodeFile(path);
What I'm doing here is naming a path for a new image and capturing the screen using the command /system/bin/screencap -p FILEPATH. Then I read the image I stored in that file and use it in the bitmap.
My problem with my current code is that it's slow(not suitable for a real-time application). I'm now trying to make it faster. Instead of saving the captured picture into file and then reading it again from the program, I want to read it directly from the result of Runtime.getRuntime().exec(...)
In the description of the command screencap, I found that I can use it without specifying the output file name, and in this case the results will be printed to stdout.
I tried several codes to read the result byte array to use it directly in my code
final Bitmap x = BitmapFactory.decodeByteArray(resultArrayByte, 0, resultArrayByte.length);
but none of the codes worked with me.
How can I use sh's input/output streams to get the result byte array directly without saving the output into a file then loading it again?
Take a Look here, in this link you can find a library called ASL
a lot of questions in this post, i'm confused :)
i hope this link is useful for your requirements.

Run shell command("su") for current screen View

I am trying to execute a shell script from my Android application to get current screen view.
I have got following code working properly on rooted devices only.
Process sh = null;
try
{
Thread thread=new Thread(new Threadcalling ());
thread.start();
sh = Runtime.getRuntime().exec("su");
OutputStream os = sh.getOutputStream();
os.write(("/system/bin/screencap -p " + "/sdcard/img.png").getBytes("ASCII"));
os.flush();
os.close();
sh.waitFor();
}
catch (Exception e){}
What modification should I do to get screen View on non-rooted devices.
Any help will be appreciated. Thank you.
You cannot do this for non-rooted devices, in other words, you app has to be a system app to access other system apps.
EDIT: Maybe this link may help you:
https://code.google.com/p/android-screenshot-library/

Android Take Screenshot rooted phone

I am capturing screenshot from my android rooted device using
Process process = 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();
process.waitFor();
It works fine as long as the content on the screen is static (image etc). But when I try to take a screenshot when a video is playing, then the screenshot shows a black screen. Is there any other way to do this?

Mount R/W system in android application to edit Read Only files

How can I, within my application, edit a file in the /system/ directory?
Do I have to make the system R/W accessible?
I ve tried:
process = Runtime.getRuntime().exec("su");
os = new DataOutputStream(process.getOutputStream());
os.writeBytes("mount -o remount,rw /system\n");
os.writeBytes("exit\n");
os.flush();
process.waitFor();
and many other ways, without success.
If anybody can help me, I'd greatly appreciated it! :)
Also, if i finally made it, will it worked with all rooted phones? Or is it different with some phones?
I use:
os.writeBytes("mount -o remount rw /system/\n");
//instead of a comma, I have a space.
//When I tried it with a comma, mine didn't work either.
And that allows me to successfully mount.
If you exit right after that, of course it will not work. You have to stay within the same process and use the linux commands to edit the file.
I have no idea how to edit the files, but I suggest googling how to do things in linux terminal, and then putting the proper code in os.writeBytes("CODE_HERE");
Though, as far as the mounting process is concerned, I don't know if that command will work universally. It may just fortunately work on my device.
EDIT:
I now use RootTools: http://code.google.com/p/roottools/downloads/list
And here is the Wiki page:
http://code.google.com/p/roottools/w/list
But I now am using:
RootTools.remount(file, mountType);
//For example:
RootTools.remount("/system/", "rw");
I believe that is universal
Edit: All version of codes below DOES NOT mount system as RW.
*Read comments below to see why.
Solution of this is not a simple command.
Edit1: I went on Super User apk, Settings tab, and "tapped" at the last item, to update the su binary. With that update, everything below isnt working.
Edit2: started a whole conversation with my self here. Fix for the current latest binary is at the bottom of the post
==================================================================================
Found out how to do it! Second day of efforts, and finally found it!!!!!
Tried several things, and answer was to a simple change mode,
what i have done:
First Version Code:(doesnt work)
String[] mountRW = { "su", "-c",
"chmod 777 /system/etc/build.prop"};
String[] mountRO = {"su", "-c",
"chmod 755 /system/etc/build.prop"};
//TODO REMOVE testing purposes
File file2 = new File("/system/build.prop");
//Make file Read-Write
process = Runtime.getRuntime().exec(mountRW);
process.waitFor();
//TODO REMOVE testing purposes
Log.d("MOUNT RW?", "RW WRITABLE? "+ file2.canWrite());
///////////////////////
// process the file
//////////////////////
// After editing finish,
//make Read Only file again
process = Runtime.getRuntime().exec(mountRO);
process.waitFor();
//TODO REMOVE
Log.d("MOUNT RO?", "RO WRITABLE? "+ file2.canWrite());
I didnt paste some try catch cases.
Also i got another problem.. And i solved it in Version 2. THe little problem was, that, i was asking for a specific for a su command, and the user, had to accept SU cmd for RO, SU cmd for RW.. and another time for other stuff in my program.
In 2nd version i m using the generic su command, so user has to accept SU privileges only ONE time, and i m using output stream.
Code Version 2(Recomended) (doesnt work):
String mountRW = "chmod 777 /system/build.prop";
String mountRO = "chmod 755 /system/build.prop";
//TODO REMOVE
File file2 = new File("/system/build.prop");
//Make file Read-Write
process = Runtime.getRuntime().exec("su"); //Generic SU Command
os = new DataOutputStream(process.getOutputStream());
os.writeBytes(mountRW + " \n");
os.writeBytes("exit\n");
os.flush();
process.waitFor();
//TODO REMOVE
Log.d("MOUNT RW?", " RW WRITABLE? "+ file2.canWrite());
////////////////////////////
/// mod the file
///////////////////////////
// After editing finish, make Read Only file again
process = Runtime.getRuntime().exec("su");
os = new DataOutputStream(process.getOutputStream());
os.writeBytes(mountRO + " \n");
os.writeBytes("exit\n");
os.flush();
process.waitFor();
//TODO REMOVE
Log.d("MOUNT RO?", "RO WRITABLE? "+ file2.canWrite());
Both codes require Root on your device.
Both versions doesnt include catch cases. (Eclpise will found them for you)
Check out your logcat(adb logcat), to see that indeed it works!
With latest su binary, this code changes slightly. The change mode command requires 4 digits. 0777 for rw permissions, and 0755 for ro permissions!
This code by its own, it does nothing to your device.
Only it mounts built.prop RW, and then mounts it back to RO.
Although if you change it, you may brick your device! Take care!

Categories

Resources