Wednesday, February 16, 2011

[android-developers] Problem changing permission with chmod and runtime.getRuntime

Hi:

I'm trying to change file permissions with chmod from code using
runtime.getRuntime(). The problem is it runs ok in my smartphone
(Samsung GT-I5700) but in other devices (Motorola Cliq) it runs ok in
a few times and in other times it not change the permissions. Any
idea? Thanks.

Here is my code:

//Coypy program1 to app folder

is = getAssets().open("program1");
fos = new FileOutputStream(getDir("", MODE_WORLD_WRITEABLE)+"/
program1");
tmp = new byte[1024];

while ((l = is.read(tmp)) != -1) {
fos.write(tmp, 0, l);
fos.flush();
}

fos.close();
is.close();

//Coypy script.sh to app folder

is = getAssets().open("script.sh");
fos = new FileOutputStream(getDir("", MODE_WORLD_WRITEABLE)+"/
script.sh");
tmp = new byte[1024];

while ((l = is.read(tmp)) != -1) {
fos.write(tmp, 0, l);
fos.flush();
}

fos.close();
is.close();

//Set permissions rwxrwxrwx to program1
commands = new String[] {"chmod","777", getDir("",
MODE_WORLD_WRITEABLE)+"/program1"};
Process process = Runtime.getRuntime().exec("su");
StringBuffer res = new StringBuffer();
DataOutputStream os = new
DataOutputStream(process.getOutputStream());
DataInputStream osRes = new DataInputStream(new
BufferedInputStream(process.getInputStream(),8192));
for (String single : commands) {
os.writeBytes(single + " ");
os.flush();
}
os.writeBytes("exit\n");
os.flush();
os.close();
process.waitFor();
osRes.close();
try { process.destroy(); } catch (Exception e) { /* nothing
*/ }

//Set permissions rwxrwxrwx to script.sh
commands = new String[] {"chmod","777", getDir("",
MODE_WORLD_WRITEABLE)+"/script.sh"};
process = Runtime.getRuntime().exec("su");
res = new StringBuffer();
os = new DataOutputStream(process.getOutputStream());
osRes = new DataInputStream(new
BufferedInputStream(process.getInputStream(),8192));
for (String single : commands) {
os.writeBytes(single + " ");
os.flush();
}
os.writeBytes("exit\n");
os.flush();
os.close();
process.waitFor();
osRes.close();
try { process.destroy(); } catch (Exception e) { /* nothing
*/ }

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment