In my application I'm trying to execute a native code which is present on my SD card.
File sdCard = getExternalFilesDir(null); // directory where native file is placed
String nativeFile = "nativeFile";
String cmd = "shell /system/bin/chmod 0777 " + sdCard.getAbsolutePath() + "/" + nativeFile;
Process proc = Runtime.getRuntime().exec(cmd);
But as soon as Runtime.getRuntime().exec(cmd) is executed, it throws error:
java.io.IOException: Error running exec(). Command: [shell, /system/bin/chmod, 0777, /storage/emulated/0/Android/data/com.example.andridutilproject/files/native] Working Directory: null Environment: null
Any suggestions, how to resolve this?
First, you should wrap calls to exec in a try-catch-clause to catch IOExceptions.
Second, use exec(java.lang.String[]) to execute a command with parameters. For example, similar to
Runtime.getRuntime().exec(new String[]{ "shell", "/system/bin/chmod", "0777", sdCard.getAbsolutePath() + "/" + nativeFile });
The sdcard in an Android system is usually disabled for execution. Therefore even if you correctly execute the chmod command it will fail.
You can test that easily. Start the shell via USB (adb shell) and execute the chmod command. It will fail with an error message like "Bad mode".
Therefore you have to copy the file to a different location where you have write access and then set the executable bit on that copy. You can try to copy the file for example to "/data/local/tmp/" but I am not sure if that path is still usable.
Related
so im having problem with accessing root app data directory
my example dir:
/data/app/com.android.chrome-jAB96abq4RXcFrKebL0BUQ==
i want to get into /data/app/com.android.chrome without extension name to get list folder
tried /data/app/* com.android.chrome */ but got null
and here's my code
String path = "/data/app/*com.android.chrome*";
File file = new File(path);
String[] dir = file.list();
for(int i=0;i<dir.length;i++) {
Toast.makeText(this, "File: "+dir[i], Toast.LENGTH_LONG).show();}
and before that i've done changing the permission to 777 and using * com.android.chrome * goes well but my code above return null when try to listing directory
try {
Runtime.getRuntime().exec("su -c chmod 777 /data");
Runtime.getRuntime().exec("su -c chmod 777 /data/app");
Runtime.getRuntime().exec("su -c chmod 777 /data/app/*com.android.chrome*")
} catch (IOException e) { e.printStackTrace(); }
tried without * package * and still got null
sry the stars doesnt have whitespace...got to whitespace because the texteditor recognize as text format
thanks before
Your code does not work, because java.io.File can not handle wildcards.
You have to specify the exact path.
As /data is not readable for regular users Java code will never be able to list a directory in this directory (a wildcard requires directory listing).
Get the app info via PackageManager and the dataDir from the ApplicationInfo for the app you are interested in:
context.getPackageManager().getPackageInfo("com.android.chrome", 0).applicationInfo.dataDir;
source
Alternatively you can run a shell ls command with su/root that lists the /data/data directory and parse the result for the chrome app data directory.
Story
I take photos and record videos with my phone camera and keep all of them on my internal storage/sdcard. I periodically back them up on my PC, so I keep these camera photos on PC storage in sync with phone storage.
For years, I've been backing up my phone camera photos to my PC in the following way:
Plug in phone into PC and allow access to phone data
Browse phone storage → DCIM → Camera
Wait several minutes for the system to load a list of ALL photos
Copy only several latest photos which haven't been backed up yet
I figured that waiting several minutes for all photos to load is an unnecessary drag so I downloaded adb platform tools. I've added the folder bin to my Path environment variable (i.e. %USERPROFILE%\Tools\adb-platform-tools_r28.0.3) so that I can seamlessly use adb and not write its full path each time.
The script
I wrote the following script for Git Bash for Windows. It is also compatible with Unix if you change the $userprofile variable. Essentially, the script pulls camera photos between two dates from phone storage to PC.
# Attach device and start deamon process
adb devices
# Initialize needed variables
userprofile=$(echo "$USERPROFILE" | tr "\\" "/") # Windows adjustments
srcFolder="//storage/06CB-C9CE/DCIM/Camera" # Remote folder
dstFolder="$userprofile/Desktop/CameraPhotos" # Local folder
lsFile="$dstFolder/camera-ls.txt"
filenameRegex="2019061[5-9]_.*" # Date from 20190615 to 20190619
# Create dst folder if it doesn't exist
mkdir -p "$dstFolder"
# 1. List contents from src folder
# 2. Filter out file names matching regex
# 3. Write these file names line by line into a ls file
adb shell ls "$srcFolder" | grep -E "$filenameRegex" > "$lsFile"
# Pull files listed in ls file from src to dst folder
while read filename; do
if [ -z "$filename" ]; then continue; fi
adb pull "$srcFolder/$filename" "$dstFolder" # adb: error: ...
done < "$lsFile"
# Clean up
rm "$lsFile"
# Inform the user
echo "Done pulling files to $dstFolder"
The problem
When I run the script (bash adb-pull-camera-photos.sh), everything runs smoothly except for the adb pull command in the while-loop. It gives the following error:
': No such file or directoryemote object '//storage/06CB-C9CE/DCIM/Camera/20190618_124656.jpg
': No such file or directoryemote object '//storage/06CB-C9CE/DCIM/Camera/20190618_204522.jpg
': No such file or directoryemote object '//storage/06CB-C9CE/DCIM/Camera/20190619_225739.jpg
I am not sure why the output is broken. Sometimes when I resize the Git Bash window some of the text goes haywire. This is the actual error text:
adb: error: failed to stat remote object '//storage/06CB-C9CE/DCIM/Camera/20190618_124656.jpg': No such file or directory
adb: error: failed to stat remote object '//storage/06CB-C9CE/DCIM/Camera/20190618_204522.jpg': No such file or directory
adb: error: failed to stat remote object '//storage/06CB-C9CE/DCIM/Camera/20190619_225739.jpg': No such file or directory
I am sure that these files exist in the specified directory on the phone. When I manually execute the failing command in bash, it succeeds with the following output:
$ adb pull "//storage/06CB-C9CE/DCIM/Camera/20190618_124656.jpg" "C:/Users/User/Desktop/CameraPhotos/"
//storage/06CB-C9CE/DCIM/Camera/20190618_124656.jpg: 1 file pulled. 15.4 MB/s (1854453 bytes in 0.115s)
The question
I can't figure out what's wrong with the script. I thought the Windows system might be causing a commotion, because I don't see the reason why the same code works when entered manually, but doesn't work when run in a script. How do I fix this error?
Additional info
Note that I had to use // in the beginning of an absolute path on Windows because Git Bash would interpret / as its own root directory (C:\Program Files\Git).
I've echoed all variables inside the script and got all the correct paths that otherwise work via manual method.
camera-ls.txt file contents
20190618_124656.jpg
20190618_204522.jpg
20190619_225739.jpg
Additional questions
Is it possible to navigate to external sdcard without using its name? I had to use /storage/06CB-C9CE/ because /sdcard/ navigates to internal storage.
Why does tr "\\" "/" give me this error: tr: warning: an unescaped backslash at end of string is not portable?
Windows batch script
Here's a .bat script that can be run by Windows Command Prompt or Windows PowerShell. No Git Bash required.
:: Start deamon of the device attached
adb devices
:: Pull camera files starting from date
set srcFolder=/storage/06CB-C9CE/DCIM/Camera
set dstFolder=%USERPROFILE%\Desktop\CameraPhotos
set lsFile=%USERPROFILE%\Desktop\CameraPhotos\camera-ls.txt
set dateRegex=2019061[5-9]_.*
mkdir %dstFolder%
adb shell ls %srcFolder% | adb shell grep %dateRegex% > %lsFile%
for /F "tokens=*" %%A in (%lsFile%) do adb pull %srcFolder%/%%A %dstFolder%
del %lsFile%
echo Done pulling files to %dstFolder%
Just edit the srcFolder to point to your phone camera folder,
plug a pattern into the dateRegex for matching the date interval and
save it as a file with .bat extension, i.e: adb-pull-camera-photos.bat.
Double-click the file and it will pull filtered photos into CameraPhotos folder on Desktop.
Keep in mind that you still need have adb for Windows on your PC.
The problem was with Windows line delimiters.
Easy fix
Just add the IFS=$'\r\n' above the loop so that the read command knows the actual line delimiter.
IFS=$'\r\n'
while read filename; do
if [ -z "$filename" ]; then continue; fi
adb pull "$srcFolder/$filename" "$dstFolder"
done < "$lsFile"
Explanation
I tried plugging the whole while-loop into the console and it failed with the same error:
$ bash adb-pull-camera-photos.sh
List of devices attached
9889db343047534336 device
tr: warning: an unescaped backslash at end of string is not portable
': No such file or directoryemote object '//storage/06CB-C9CE/DCIM/Camera/20190618_124656.jpg
': No such file or directoryemote object '//storage/06CB-C9CE/DCIM/Camera/20190618_204522.jpg
': No such file or directoryemote object '//storage/06CB-C9CE/DCIM/Camera/20190619_225739.jpg
Done pulling files to C:/Users/User/Desktop/CameraPhotos
This time I started investigating why the output was broken. I remembered that windows uses \r\n as newline, which means Carriage Return + Line Feed, (CR+LF), so some text must have been overwritten.
It was because of broken values stored inside the $filename variable.
This is the loop from the script:
while read filename; do
if [ -z "$filename" ]; then continue; fi
adb pull "$srcFolder/$filename" "$dstFolder"
done < "$lsFile"
Since each iteration of the while-loop reads a line from $lsFile in the following form:
exampleFilename.jpg\r\n
It misinterprets the newline symbols as part of the file name, so adb pull tries to read files with these whitespaces in their names, but fails and it additionally writes a broken output.
Adb Photo Sync
This might not be the answer but might be useful for others looking for android photo/files backup solution.
I use this script on my Windows with git bash. This can be easily used for Linux. A common issue with a long backup process is that it might get interrupted and you might have to restart the entire copy process from start.
This script saves you from this trouble. You can restart the script or interrupt in between but it will resume copy operation from the point it left.
Just change the rfolder => android folder, lfolder => local folder
#!/bin/sh
rfolder=sdcard/DCIM/Camera
lfolder=/f/mylocal/s8-backup/Camera
adb shell ls "$rfolder" > android.files
ls -1 "$lfolder" > local.files
rm -f update.files
touch update.files
while IFS= read -r q; do
# Remove non-printable characters (are not visible on console)
l=$(echo ${q} | sed 's/[^[:print:]]//')
# Populate files to update
if ! grep -q "$l" local.files; then
echo "$l" >> update.files
fi
done < android.files
script_dir=$(pwd)
cd $lfolder
while IFS= read -r q; do
# Remove non-printable characters (are not visible on console)
l=$(echo ${q} | sed 's/[^[:print:]]//')
echo "Get file: $l"
adb pull "$rfolder/$l"
done < "${script_dir}"/update.files
I am writing a simple app using react native to make some file operations done automatically. I have tried packages like strong text react-native-fs, but I found that there are some files/folders that can not be read using
RNFS.ls(PATH).
Trying to list files in that folder will throw an exception.
However, these files can be displayed using ls command in adb shell. So I am wondering if there is a way that we can run shell commands in react native like we make some system calls in java/python?
Thanks
Write your own Native Module.
You can easily run a shell command from Java and call it from React Native using React Native's Native Modules. You can find more information here:
https://facebook.github.io/react-native/docs/0.60/native-modules-android#callbacks
And for your Java command, you'd want to use something like this:
String command = "ls " + path; // Where path is your desired path.
Runtime runtime = Runtime.getRuntime();
Process result = runtime.exec( command );
BufferedReader bufferedReader = new BufferedReader( new InputStreamReader( result.getInputStream() ) );
String[] parts = bufferedReader.readLine().split( "\\s+" );
#flyskywhy/react-native-android-shell works well even with root command e.g.
AndroidShell.executeCommand('su -c ifconfig eth0 down; su -c ifconfig eth0 hw ether 19:21:19:49:20:21; su -c ifconfig eth0 up', (result) => {
console.log(result)
});
So, I need to copy a file to /system, and have to use shell commands, because the application is not running with enough privileges (and is not even installed to /system/apps at that time) - so its impossible to write to /system directly.
String cmd = String.format("\"/bin/sh -c \'cp %s %s'", tempPath, out);
p = Runtime.getRuntime().exec(new String[] { "su", "-c", cmd });
However, SuperSU pop-up informs me, that following application was granted to run as root:
/bin/sh -c _cp %s %s_.
So ' are replaced with _, and eventually command takes no effect.
What is a proper way to run a command having so much quotes ("" and '' for a single argument to su)?
I know of using su's stdin to run commands separated by \n, but it seems to be having its own problems (and was not working too).
If you're calling through SuperSU, there are a ton of pitfalls, and the recommended way is to use libsuperuser. See http://su.chainfire.eu/ for details.
I have this .php file sending commands to my android application:
I have tried working with:
Runtime.getRuntime().exec(commandLine);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
but the commands echo, pwd and some others are not working.
I get the following exception:
java.io.IOException: Error running exec(). Command: [pwd] Working Directory: null Environment: null
As far as I understand this is because there is not any shell environment.
Then I have tried writing in a .sh file the command I want and then execute the command this way:
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("sh /runCmds/shTest.sh");
InputStream is = proc.getInputStream();
and got solved the problem with pwd, echo and most of the commands.
But later on I realized that I want to keep the state of the commands I execute.
For example I want to change directory(cd data) and execute the command mkdir Apoel
And here is when I face my problem. What to do?
I came up with another idea:
Make a shell script (.sh) and each time the user wants to execute a command append the new command in it (and run the hole script(.sh) again). But I think is not a very good way to do it!
Is there any easy way to it? Can my application open a terminal easily?
Here is a code I found for a Terminal Emulator, but it is too complicated!
https://github.com/jackpal/Android-Terminal-Emulator
An interactive shell is one which remains running, waiting for new commands which it receives from stdin, while producing it's output to stdout and stderr - as a result, it's environment including any changes is retained for the duration of the session. For the shell to be useful to a user, stdin, stoud, stderr need to be connected through to the user - via the console, a serial line, or xterm, etc.
On Android, typically what you do is hook onto the pipes corresponding to stdin, stdout, stderr for the shell process that you've created, and use them to push in commands provided by your java program and accept output for your program to interpret/display.
The idea of creating a script and running it would only work in the case where all the commands are entered before any of them execute.
First, some background. On any Posix system, in the shell, there are 2 types of commands:
Internal commands (pwd,cd,echo)
external commands (ls,cp,mv, sometimes echo as well)
All the directory context commands (cd,pwd etc) are commands implemented inside the shell and thus require the shell to remain running, if the changes are to hold (for eg. cd /data/local/tmp).
On the other hand, external commands are standalone and can run indepenedently, but acquire their directory context from their parent process (in most cases, the shell).
Now to solve the problem. Yes, using a script is a good idea, but it is painful to implement, as it requires an overhead of file editing. However, we can use the shell to create a script on the fly and execute it using the -c option. For example:
/system/bin/sh -c 'cd /data/local/tmp; touch abc; cp abc def; cd /; rm /data/local/tmp/abc'
In summery:
String sPrefix="/system/bin/sh -c 'cd someplace;";
String sInputCmd=getCommand(); // this is simulating your command input
String sPostfix="'";
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(sPrefix+sInputCmd+sPostfix); // sh -c 'cd someplace; echo do something'
InputStream is = proc.getInputStream();
However, this does not give us the capability to set a directory context, so we need to simulate that. Directory contexts can be added by using the Runtime.exec(String command, String[] envp, File dir) method. With this we have the possibility of maintaining directory
context between commands. But how do we actually do that? One solution is to append pwd to the command and take the last line of the output as the new directory context. Thus
String sPath = "/"; // start in root directory
String sPrefix = "/system/bin/sh -c 'cd someplace;";
String sInputCmd; // this is simulating your command input
String sPostfix = ";echo;pwd'";
Runtime rt = Runtime.getRuntime();
while(!(sInputCmd=getCommand()).equals("")) {
File dir= new File(sPath);
Process proc = rt.exec(sPrefix+sInputCmd+sPostfix, NULL, dir);
InputStream is = proc.getInputStream();
// Do processing on input.
sPath= last_line_of_is ;
}
Finally, the last option is to integrate one of the terminal emulators into you app.
[1] http://docs.oracle.com/javase/6/docs/api/java/lang/Runtime.html#exec%28java.lang.String,%20java.lang.String%5B%5D,%20java.io.File%29