Modifying a system file with a root application - android

I'm new to android app development and have a question. I need to mount the system as rw, copy a file in /system/etc (back up the file which I have done) then open the original file, (it's an xml file) parse through it and then add an element. I haven't written the part yet to parse the file but I'm just trying to see if I can write to the file. I figured before I even write the xml parsing code I should see if I can at least write to the file but canWrite returns false. Anyone have any ideas? Thanks in advance
code in onCreate:
try {
RunProcess= Runtime.getRuntime().exec("su");
} catch (IOException e) {
e.printStackTrace();
}
code in button click:
try {
DataOutputStream os;
File file2 = new File("/system/etc/permissions/platform.xml");
os = new DataOutputStream(RunProcess.getOutputStream());
os.writeBytes("mount -o rw,remount /system\n");
Log.d("MOUNT RW?", " RW WRITABLE? " + file2.canWrite());
os.writeBytes("exit\n");
os.flush();
RunProcess.waitFor();
Toast.makeText(getApplicationContext(), "Backed up original file", Toast.LENGTH_SHORT).show();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}

Related

AOSP: Write a file on /data directory

I want to write a file on "/data" directory. I have rooted my device and setenforce 0. However, I am getting:
W/System.err: java.io.FileNotFoundException: data/MyDoople.txt
(Permission denied)
Here is my code (it works for sdcard):
String filename= "MyDoople.txt";
try
{
File f = new File("data/"+File.separator+filename);
FileOutputStream fOut = new FileOutputStream(f);
OutputStreamWriter myOutWriter = new OutputStreamWriter(
fOut);
myOutWriter.append("Mytest");
myOutWriter.close();
fOut.close();
}
catch(Exception e)
{
e.printStackTrace();
}
Do I have to add any policy to make it work?
Because the permission of the data directory is rwxrwx--x, user system, group system, and the running user of your app is a common user, not a system user, so the app unable to read and write the /data directory directly.
Two ways can be referred to:
1: If you have a platform certificate, declare android:sharedUserId="android.uid.system" in AndroidManifest.xml, and use the platform certificate to re-sign the app. In this way, the user running your app is the system user, who has read and write permissions to the /data partition.
2: obtain the root permission in the app, and then execute the relevant command, refer to:
public static boolean RootCommand(String command) {
Process process = null;
DataOutputStream os = null;
try {
process = Runtime.getRuntime().exec("su");
os = new DataOutputStream(process.getOutputStream());
os.writeBytes(command + "\n");
os.writeBytes("exit\n");
os.flush();
process.waitFor();
} catch (Exception e) {
Log.d("*** DEBUG ***", "ROOT REE" + e.getMessage());
return false;
} finally {
try {
if (os != null) {
os.close();
}
process.destroy();
} catch (Exception e) {
}
}
Log.d("*** DEBUG ***", "Root SUC ");
return true;
}

How to properly write file in android to make it available to ADB?

Actually i have an app that by receiving a specific intent write a specific txt file with a written in it trace.
To write the file i use the following method
public void write(String data, Context context,String filename) {
try {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(context.openFileOutput(filename, Context.MODE_PRIVATE));
outputStreamWriter.write(data);
outputStreamWriter.close();
} catch (IOException e) {
Log.e("Exception", "File write failed: " + e.toString());
}
}
And till here all is working fine, the file is written but now i need to pull that file via ADB and here come the issue. As the OutputStreamWriter is set to Context.MODE_PRIVATE adb pull command just can't find that file so which would be the best method to write that file in a "public" directory and then be able to pull that file via ADB?
The ADB commands will be sent from a VB.NET programm.

Android Studio - Acces to /storage/emulated/0 from windows explorer and android file manager

I a newbie in android development.
I sure that many people have same issue. I ve a Galaxy S7 phone without SD Card. So no external storage. But i want create file with my app which have to be access from windows explorer to export it.
Note : debbug in USB mode - No virtual device
Of course in my manifest file i ve setted those 2 permissions :
android.permission.WRITE_INTERNAL_STORAGE
android.permission.WRITE_EXTERNAL_STORAGE
To be sure i ve created a file i use this write method and the following read method :
File root = this.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS);
File customFolder = new File(root.getAbsolutePath() + "/CustomFolder");
customFolder.mkdirs();
file = new File(customFolder, "myData.txt");
// Must catch FileNotFoundException and IOException
try {
FileOutputStream f = new FileOutputStream(file);
PrintWriter pw = new PrintWriter(f);
pw.println("Howdy do to you,");
pw.println("and the horse you rode in on.");
pw.flush();
pw.close();
f.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
Log.i(TAG, "File not found");
} catch (IOException e) {
e.printStackTrace();
Log.i(TAG, "I/O exception");
}
To read myData.txt
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while (true) {
line= br.readLine();
if (line== null) break;
tv.append("\n" + " " + line);
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
So all method are OK, but when i browse my files system to get the created file myData.txt, I can't find it !!
Most of the apps we install have their own folder on the root, like Snapchat, Whatsapp etc etc
I d like to make the same thing, what is the way to write file in:
ROOT --> MyApplicationName --> CustomFolder
Thanks for your help :)
I finally found a solution adding a MediaScannerConnection.scanFile() method.
MediaScannerConnection.scanFile(getApplicationContext(),new String[]{file.getAbsolutePath()},null,new MediaScannerConnection.OnScanCompletedListener() {
#Override
public void onScanCompleted(String path, Uri uri) {
// Do something here if you want
}});
But this solution don't totally fit me, because the storage folder is :
Android/data/donain_name/files/Documents/CustomFolder
I'd like to have same result like whatsapp application which can have a folder in the internal storage root.
The result i want is :
MyApplication/CustomeFolder

android open file in root directory

Is there any way to write and read text files on rooted Android phone in root directories (ex. /data/)?
InputStream instream = openFileInput("/data/somefile");
doesn't work
You can only access to /data folder is you're root user.
Call to SU binary and write bytes (these bytes are the command) over SU binary via OutputStream and read command output via InputStream, it's easy:
Call to cat command to read files.
try {
Process process = Runtime.getRuntime().exec("su");
InputStream in = process.getInputStream();
OutputStream out = process.getOutputStream();
String cmd = "cat /data/someFile";
out.write(cmd.getBytes());
out.flush();
out.close();
byte[] buffer = new byte[1024 * 12]; //Able to read up to 12 KB (12288 bytes)
int length = in.read(buffer);
String content = new String(buffer, 0, length);
//Wait until reading finishes
process.waitFor();
//Do your stuff here with "content" string
//The "content" String has the content of /data/someFile
} catch (IOException e) {
Log.e(TAG, "IOException, " + e.getMessage());
} catch (InterruptedException e) {
Log.e(TAG, "InterruptedException, " + e.getMessage());
}
Don't use OutputStream for write files, OutputStream is used for execute commands inside SU binary, and InputStream is used for get output of command.
To be able to do what you are asking you must do all of the operations via the SU binary.
like...
try {
Process process = Runtime.getRuntime().exec("su");
process.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
Reading will be easier then writing, for writing easiest would be to write the file to some place where u have access using standard java api's and then move it to the new location using the su binary.

Write to a file android

I am trying to write some date data in a file with with function:
public void writeSettings(Context context, String data){
FileOutputStream fOut = null;
OutputStreamWriter osw = null;
try {
fOut = openFileOutput("settings.dat",MODE_PRIVATE);
osw = new OutputStreamWriter(fOut);
osw.write(data);
osw.flush();
Toast.makeText(context, "Settings saved",Toast.LENGTH_SHORT).show();
}
catch (Exception e) {
e.printStackTrace();
Toast.makeText(context, "Settings not saved",Toast.LENGTH_SHORT).show();
}
finally {
try {
osw.close();
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
In the blog where I found this code, it is said my file is stored in:
/data/data/PACKAGE_NAME/files/settings.dat
But I cannot find it on my phone! Where is this data folder?
In phone (Device) you don't have root privileges,so you can't see /data/data/ directory using eclipse->DDMS->File Explorer,
SO if you want to just see whether your file is created or not you can use ./adb shell command (for linux) adb shell (for windows)
Or try this on emulator and check with eclipse -> DDMS -> File Explorer...
You don not have access to /data folder in your phone unless you have root privileges.
You can test your app in emulator, data folder in emulator is accessable.
Have you tried to add permissions WRITE_SECURE_SETTINGS and/or WRITE_SETTINGS?
If you are using eclipse ide, find DDMS Window and click File Explorer. Search for /data/data/Package_Name/files

Categories

Resources