I am working in android application. I want to copy data from 1 file to other file.I have executed below command for it, but not able to copy data from file1 -> file 2.
// Executes the command.
String CAT_COMMAND = "cat /sdcard/file1 > /sdcard/file2";
Process process = Runtime.getRuntime().exec(CAT_COMMAND);
I have used ADB SHELL command at DOS , executed CAT command which works fine.
But code execution is not working into the real devices.
please help me out why does it is not copying data from file1->file2.??
provide resolution for writing/copying data using CAT command.
Thanks
can you try with the complete path /mnt/sdcard .(or) Try to get the right path by Environment.getExternalStorageDirectory()
Related
Basically, I dump all data into the directory 'sdcard/2019-07-xx-xx and there are subdirectories in 2019-07-xx-xx. The Folder is named after the timestamp when dumping data, now I run the command:
adb pull '/sdcard/2019-07*'
but it prompted:
adb: error: failed to stat remote object 'sdcard/2019-07*': No such file or directory
who can help me out?
Example:
for days in 0{1..9} 1{0..9} 2{0..9} 3{0..1}; do
adb shell echo "\$EXTERNAL_STORAGE/2019-07-$days/"
done
Gives us below output:
/sdcard/2019-07-01/
/sdcard/2019-07-02/
/sdcard/2019-07-03/
/sdcard/2019-07-04/
/sdcard/2019-07-05/
......
Final: You can copy and paste
- don't forget to change source_path and dates if needed - Notice the trial slash:
for days in 0{1..9} 1{0..9} 2{0..9} 3{0..1}; do
adb pull "\$EXTERNAL_STORAGE/2019-07-$days/" ~/source_path
done
According to documentation "adb pull" command can pull file or directory from your device. There is no support for a wildcard.
But you can use xargs to accomplish this. Just check post
my environment:
cpu:rk3288
os:android7.1
transfer method:sftp
I wrote android code to do these things below:
get the logcat with code "adb logcat -d -v time -f /mnt/sdcard/logcat.txt"
pull the file logcat.tx to the server with sftp
in 1st step I coding some java language with android studio like below, if anyone can help me, thanks!
Process p = Runtime.getRuntime().exec("adb logcat -d -v time -f /mnt/sdcard/logcat.txt");
error massage:
java.io.IOException: Cannot run program "adb": error=13, Permission denied
You can't use adb commands from inside the device, even if you somehow have it in the device you would need root permissions. The adb is the bridge between the PC and the device. Take a look here: https://developer.android.com/studio/command-line/adb
Although you probably can just remove the adb and use logcat directly, like:
Process p = Runtime.getRuntime().exec("logcat -d -v time -f /mnt/sdcard/logcat.txt");
Here you can see some more options about using the logcat command: Read logcat programmatically within application, including Reetpreet Brar's answer, that I think will be better for you:
Process pq=Runtime.getRuntime().exec("logcat v main"); //adapt the command for yourself
BufferedReader brq = new BufferedReader(new InputStreamReader(pq.getInputStream()));
String sq="";
while ((sq = brq.readLine()) != null)
{
//here you can do what you want with the log, like writing in a file to
//send to the server later.
}
Here you can choose methods to write your file: How to Read/Write String from a File in Android
Then, just send the file to the server.
I'm using xamarin forms to develop an Android app. I can save a file via
var fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), $"appsettiings.txt");
var data = JsonConvert.SerializeObject(this);
File.WriteAllText(fileName, data);
When I debug I can see that the file should be stored at
/data/user/0/<applicationame>/files/.local/share/appsettiings.txt
I like to see if the file is actually saved and what the content is. I opened the Android device monitor but the data folder was 'empty'. From some other SO case I took that I should but myself in root-mode by executing
C:\Program Files (x86)\Android\android-sdk\platform-tools>adb root
Now the data folder contains data and I can drill down up to the files folder, but that one seems to be empty.
When I run the app again and check in code if the file exists, it actually does.
Any further suggestions how to get access to that file from any tooling. I want to delete that file and run the app again.
You can use adb pull to copy the file to local machine and look at it content like this:
adb root
adb pull /data/user/0/<applicationame>/files/.local/share/appsettiings.txt [LOCAL_FOLDER]
then you can use adb shell then rm -f to remove it, like this:
adb shell
su
rm -f /data/user/0/<applicationame>/files/.local/share/appsettiings.txt
Since the file you are requesting is inside /data folder, you need to have the corresponding root priviliege to get it, so you need to do adb root before the adb pull command, and you need to do su before removing the file.
If I am not mistaken, if you save a file with your approach, it should exist, as you already proofed with the .Exists method.
I am not quite sure, are you just trying to read the content out of the file? Or do you want to access it from outside of the application?
If your desire is to just read the content, you could access it with
string content= File.ReadAllText(your file path);
and set a breakpoint (F9) on the next line.
When using uiautomator, takeScreenshot(File storePath) always returns false no matter what parameter I pass in.
I've tried to give either new File(dir_name, file_name) or new File(file_name), neither of them works (of course mkdir first if the dir_name doesn't exist).
Every time it just return false and /data/local/tmp/ on emulator is empty.
BTW, I don't think it's a permission problem, since trying the similar dumpWindowHierarchy could generate a dump file there.
Thanks in advance for your help.
If you are using emulator to run the tests you should turn on "Use Host GPU" in your AVD configuration. After this change it worked for me.
If you still got a problem you may try screencap. It is command line tool for taking screenshots. It works in both emulator settings. To save screenshot in given path execute:
Process process = Runtime.getRuntime().exec("screencap <path>");
process.waitFor();
Use /data/local/tmp to avoid problems with permission. You can use SD Card dir as well. It is asynchronous so wait until process is finished with waitFor(). It will recognize desired output format by extension of provided file.
Or you can get PNG in InputStream (no need to wait):
Process process = Runtime.getRuntime().exec("screencap -p");
InputStream output = new BufferedInputStream(process.getInputStream());
You can omit -p if you wish to get file in JPEG. They JPEG screenshots are bigger, but it takes less time to get them.
The takeScreenshot() method is applicable from 4.2 and above android version devices
If the device version is appropriate, then use the following piece of code
File path = new File("/sdcard/filename.png");
int SDK_VERSION = android.os.Build.VERSION.SDK_INT;
if (SDK_VERSION >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
mUiAutomatorTestCase.getUiDevice().takeScreenshot(PATH);
}
We can view the file by following command
$ adb shell ls -l /sdcard/name-of-file
I am trying to access SQLite database from command line for an android application I have developed. I type adb shell to connect to the device and then sqlite3 to connect to the database. sqlite3 gives me the following an error:
/system/bin/sh: sqlite3: not found
Try typing "adb shell type sqlite3" to see whether sqlite3 is available in your device or not.
For my device, it's showing as follows
$ adb shell type sqlite3
--> sqlite3 is a tracked alias for /system/xbin/sqlite3
// Below code works on windows platform.. tried on windows 7 and windows 8
// This is vb script code.. save with .vb extention.
// any problems in saving.. please copy paste any vb file from ur system,take that second file ,open it with notepad and clear all contents, paste the below code as i did same.
// double click to run file. output shown will ur all tables. and u can continue with the own sql statements
// note : works with only one emulator. many emulator are supported.
// please dont try this code with mobile, you wont be able to access mobile database
// start of script
Set WshShell = WScript.CreateObject("WScript.Shell")
// set ur path to platform tools in ur sdk
strApp = "E:\Android\sdk\platform-tools"
arrPath = Split(strApp, "\")
For i = 0 to Ubound(arrPath)
strAppPath = strAppPath & arrPath(i) & "\"
Next
WshShell.CurrentDirectory = strAppPath
WshShell.Run ("cmd.exe")
WScript.Sleep 50
WshShell.SendKeys "adb shell"
WshShell.SendKeys "{ENTER}"
// put ur package name and database name over here. please put ur db name with .db extension
WshShell.SendKeys "sqlite3 /data/data/package_name/databases/database_name"
WshShell.SendKeys "{ENTER}"
WshShell.SendKeys ".tables"
WshShell.SendKeys "{ENTER}"
Set objShell = Nothing
// script ends