Acess files on root in device - android

I'm developing an application for Galaxy Tab, and now I need get some files on the /data/data// folder.
The emulator is really, really slow. So I'm testing on device, and need get the files from device (from emulator i'm able to do). How can I do that?

Add a debug function in your code to copy the required files from the protected folders onto your phone's SD card. You can then access them from your PC.
public static void backupFile() throws IOException {
String inFileName = "/data/data/your.package.name/........"; //TODO Use folder/filename
File inFile = new File(inFileName);
FileInputStream fis = new FileInputStream(inFile);
String outFileName = Environment.getExternalStorageDirectory()+"/........"; //TODO Use output filename
OutputStream output = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer))>0)
output.write(buffer, 0, length);
output.flush();
output.close();
fis.close();
}

The /data directory on a device has restricted permissions. To copy files off using adb you will need to have rooted your device.

adb pull <path>
will download files from the device to your current directory.
You can also use adb shell to explore your device using common shell commands. adb shell ls <path> will let you do this without actually entering the shell completely.
Edit - this will not work for files in the /data directory or other restricted directories unless you have root access to the device.

Related

Config file on Android

New to Android, working on an app for Vuzix M300s. The app needs to access a file that contains the IP address and port of a web server.
I believe I will need to manually place a pre-configured file on the M300s using adb shell, but I cannot figure out where on the device to place it so that the app can find it.
Via Android Studio 3.1.3, I have placed a file in the assets folder which I can open & read, but using adb shell I cannot locate it. (I get permission denied for a lot of actions like ls).
How do I get a file on there? Or is there a better way?
Note that the assets folder in your project only exists on your development machine. The contents of this folder are packaged into the APK file when you build your app. In order to read any of these files, you need to use Context.getAssets() as explained in read file from assets.
Figured it out.
To move/copy a file to the M300s for an application
move the file to the device (in the sdcard folder)
.\adb push C:\temp\file.cfg /sdcard/
move the file from /sdcard/ to the desired location
a) go into the shell
'> .\adb shell
b) change to the application's permissions
$ run-as com.foobar.appname
c) copy the file into the app's 'files' folder
$ cp /sdcard/file.cfg files/
Within my app, I was able to read this with
FileInputStream fin = openFileInput("file.cfg");
InputStreamReader rdr = new InputStreamReader(fin);
char[] inputBuffer = new char[100];
int charsRead = rdr.read(inputBuffer);
String fileContents = new String(inputBuffer);
rdr.close();
Log.i(method, "charsRead: " + charsRead);
Log.i(method, "fileContents: " + fileContents);

How do I grant super user permission to fileinputstream object and read all system files on my rooted device

I have a rooted device I am trying to read files from a specific folder /sdcard/videos using FileInputStream and successfully created a CHECKSUM value for that folder, Now I want to read all the files from my system folder and create a checksum value for it but when I pass the folder path which is /system I am unable to read few files and get the following error:
java.io.FileNotFoundException: system/bin/run-as: open failed: EACCES (Permission denied)
How do I overcome this, how do I grant super user permission or root access to read all the system related files ?
Simplified: Programatically Read file from sdcard/system folder using fileinputstream on rooted device
Havent tried it yet
Process p = Runtime.getRuntime().exec(new String[]{"su", "-c", "system/bin/sh"});
DataOutputStream stdin = new DataOutputStream(p.getOutputStream());
//from here all commands are executed with su permissions
stdin.writeBytes("md5sum filepath");
/* executes the md5sum binary command,replace with installation path of md5sum after you install busybox */
InputStream stdout = p.getInputStream();
byte[] buffer = new byte[BUFF_LEN];
int read;
String out = new String();
//read method will wait forever if there is nothing in the stream
//so we need to read it in another way than while((read=stdout.read(buffer))>0)
while(true){
read = stdout.read(buffer);
out += new String(buffer, 0, read);
if(read<BUFF_LEN){
//we have read everything
break;
}
}
Create checksum by calling the md5sum binary
ie. If you have BusyBox installed on your device

Unable to View or pull SQLite database from device [duplicate]

This question already has answers here:
Retrieve database or any other file from the Internal Storage using run-as
(12 answers)
Closed 4 years ago.
I am unable to View or Pull my SQLite database file created by my application on my device.
Here is how I tried to pull it using ADB
E:\>adb pull /data/data/com.example.testinglist/databases/timetracker.db
failed to copy '/data/data/com.example.testinglist/databases/timetracker.db' to
'./timetracker.db': **Permission denied**
As you can see there is a Permission denied error.
I can't see or copy file using FileExplorer in Eclipse ADT. Only folder data can be copied as unable to move in the folder in FileExplorer. and folder Data copied to computer using FileExplorer is empty. Any solutions please??
If your device isn't rooted, you may use this code to export your applications database onto your sdcard:
try {
File dstDb = new File(dstPathOnSdcard, dstFileName);
FileOutputStream output = new FileOutputStream(dstDb);
File srcDb = new File(Environment.getDataDirectory(), "//data//com.mynamespace.myapp//databases//mydatabasename.db");
FileInputStream input = new FileInputStream(srcDb);
FileChannel src = input.getChannel();
WritableByteChannel dst = Channels.newChannel(output);
src.transferTo(0, src.size(), dst);
src.close();
input.close();
dst.close();
output.close();
}
catch (Exception e) {
// doSomething
}
This is how I do it (on an ubuntu machine, without root):
Locate adb file in Android/Sdk/platform-tools/
Open command line in this directory
Run the following code, to pull Sqlite database from app to SD card:
./adb -d shell "run-as com.example.name.myappname cp /data/data/com.example.name.myappname /databases/database.db /sdcard/database.db"
*Make sure to replace "database.db" and "com.example.name.myappname", by your databasename and app name (found in your manifest).
Run the following code, to pull the Sqlite data from SD card to desktop:
./adb -d pull /sdcard/database.db ~/Desktop/database.db

Install .APK from Android Cache

I have problems installing an APK saved in Android internal Cache.
There are no issues saving the file in External Storage or on External Cache using context.getExternalCacheDir().
But if I try to use context.getCacheDir(), the log returns
/data/data/com.my.package/cache/update.apk: open failed: EACCES (Permission denied)
File file = context.getCacheDir();
File outputFile = new File(file, "update.apk");
if(outputFile.exists()){
outputFile.delete();
}
FileOutputStream fos = new FileOutputStream(outputFile);
InputStream is = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = is.read(buffer)) != -1) {
fos.write(buffer, 0, len1);
}
fos.close();
is.close();
Intent intent = new Intent(Intent.ACTION_VIEW);
//SAVE IN CACHE
intent.setDataAndType(Uri.fromFile(outputFile), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // without this flag android returned a intent error!
context.startActivity(intent);
It looks like internal cache doesn't allow the APK to be correctly read.
The fact is, if the file is saved on the External Storage or external Cache the APK will be available to the user, and I don't want that.
What can it be don to save the file in internal Cache?
Thanks
It looks like internal cache doesn't allow the APK to be correctly read.
That is because the installer app has no rights to read your file.
The fact is, if the file is saved on the External Storage or external Cache the APK will be available to the user, and I don't want that.
Then do not install it on the user's device. Any user can copy any APK off their device at any time after installation, so the only way to prevent the user from accessing the APK is to not have it on the device in the first place.
What can it be don to save the file in internal Cache?
Probably nothing. If you switch to openFileOutput(), you can see if MODE_WORLD_READABLE will be sufficient for the installer to proceed. Again, this will not stop the user from being able to access the APK file.
Try to look chmod command to get read\write permissions on internal folder... As for linux it looks like...
chmod 777 /data/data/*** or chmod 644 /data/data/***

How to execute linux command in android programmatically?

As in android(through android sdk/tools folder) from command line we can execute linux shell command to access mnt folder/data folder likewise. (e.g cd data ls) now that command i want to execute from programmatically in android so how could it be possible?
I am using following code to execute shell command
Process p = Runtime.getRuntime().exec("cd data");
but it is giving me exception
java.io.IOException: Error running exec(). Command: [cd, data] Working
Directory: null Environment: null
so how should i proceed for it.Thanks in advance.
cd is not a Linux command, it's a command built into the shell; it changes the current working directory in the context of that shell process. In your case, if the command were to be successful, it would be successful for the child process only (which would soon terminate) and would have no effect on your own process.
On Android, your process does not have permissions to read files in other app's /data/data/_other-package-name_, or list its private files in directory /data/data/_other-package-name_/files. But it does have permission to list and read files in the lib directory /data/data/_other-package-name_/lib, and you can look at a specific file in /data/data/_other-package-name_/files, if the other-package opened this file as public.
I.e. if the other-package does something in line with:
FileOutputStream fos = openFileOutput("public_file", Context.MODE_WORLD_READABLE);
fos.write("hello world".getBytes());
fos.close();
then your package can read this file like this:
byte[] bytes = new byte[100];
FileInputStream fis = new FileInputStream(new File("/data/data/*other-package*/files/public_file"));
int cnt = fis.read(bytes);
fis.close();
Log.d("Two_Libs", new String(bytes, 0, cnt));
But you cannot list the public files in that directory to discover them.
try this :
Process p = Runtime.getRuntime().exec("cd /data");
To retrieve the path to your app's private data folder use the following from Java:
File MyData = Ctxt.getDir("Foo");
Where Ctxt is a Context object, like an Activity. It will return you a path like /data/data/com.activity.networkRequestDetector/app_Foo. Note that reading/writing /data/data/com.activity.networkRequestDetector/ is discouraged in Android - it's your application's sandbox's root, not to be played with.
To open files from the data folder, use something like this:
FileInputStream Stm = new FileInputStream(new File(MyData, "Filename.txt"));
In general, anything a shell command does your app can do, too. Shell commands are just programs that use API like everyone else.
Use as following:-
Process process = Runtime.getRuntime().exec("command to be executed");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));

Categories

Resources