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

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/

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();

Read private file (/data/data folder) in rooted device

I need to access a file contained in the private folder of another app. I have granted my app the root privilege and changed the permissions - although I think it's not necessary - but when I try to read from the file, I get "permission denied".
This is the code:
File file = new File("/data/data/other.app/shared_prefs/file.xml");
if(file.exists()) {
try {
Runtime.getRuntime().exec("su");
Runtime.getRuntime().exec("chmod 777 " + file.getAbsolutePath());
InputStream in = new FileInputStream(file);
....
} catch (IOException e) {
e.printStackTrace();
}
}
Probably you wont be able to do this, because each app on android has a user with unique permissions.
See this: http://developer.android.com/guide/topics/security/permissions.html#userid
Any data stored by an application will be assigned that application's user ID, and not normally accessible to other packages. When creating a new file with getSharedPreferences(String, int), openFileOutput(String, int), or openOrCreateDatabase(String, int, SQLiteDatabase.CursorFactory), you can use the MODE_WORLD_READABLE and/or MODE_WORLD_WRITEABLE flags to allow any other package to read/write the file. When setting these flags, the file is still owned by your application, but its global read and/or write permissions have been set appropriately so any other application can see it.
You cannot break up the su and the chmod operations like that.
This code:
Runtime.getRuntime().exec("su");
Runtime.getRuntime().exec("chmod 777 " + file.getAbsolutePath());
Does not result in the chmod being executed in a root shell. Each call to exec kicks off a NEW process.
You need to run the commands you need all within a single process. The easiest way to do that is to write a shell script to your /data/data directory that does these operations for you and then run that through the sh shell processor.
Please note that good security practice would be to chmod the file back to not world readable after you are done with it in your app so that you are not leaving the other app exposed forever.
This answer looks to have what you need: Run binary from with root Android Application
SOLVED
Solved using this code to run the commands:
public static void runAsRoot(String[] cmds){
try {
Process p = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(p.getOutputStream());
for (String tmpCmd : cmds) {
os.writeBytes(tmpCmd+"\n");
}
os.writeBytes("exit\nexit\n");
os.flush();
p.waitFor();
} catch(IOException e) {
e.printStackTrace();
} catch(InterruptedException e) {
e.printStackTrace();
}
}

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?

Take really quick screenshots of Android Phone programmatically

I am taking screenshots of an Android phone from my application by this code
java.lang.Process process = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(process.getOutputStream());
os.writeBytes("cat /dev/graphics/fb0 > " + raw + "\n");
os.writeBytes("exit\n");
os.flush();
os.close();
process.waitFor();
But when I put this in a service, and try to run infinitely, I get atmost 2 screenshots.
Is there a way to take screenshots really quick? Like 15-20 in a second using ANY way possible?
EDIT : I try to convert them to video later. Any way to make video without dumping it to the file altogether?

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