1

I want to set permission on a file of sdcard that no one can delete the same, for this I want to run chmod 400 command on that file, but I don't know how to do that programmatically in android. Please suggest me any solution for the same.

Thanks in advance.

Sanat Pandey
  • 3,961
  • 16
  • 69
  • 126

1 Answers1

2

This is not supported. The SD card uses a FAT filesystem without per-user permissions. Even if you were able to do a chmod 400 (which you may be able to do with the Runtime.exec(), or File.setReadOnly() method or similar), it may not be a good idea to do it directly on the SD card filesystem, because nothing prevents someone from simply marking it read/write again.

You should use the official data storage APIs, which should be sufficient for your needs. (and more secure assuming a non-rooted device)

mpontillo
  • 13,147
  • 7
  • 56
  • 89
  • Hi Mike.. can you tell me how to use chmod command in android programmatically? I am trying to give Runtime.getRuntime().exec("chmod 777 /dev/ttyS0") but it is not working eventhough I have the root permissions for ttyS0.. – Siva Kumar Feb 11 '13 at 08:39
  • @SivaKumar, on my Android device, `chmod` is a symbolic link to `/system/bin/toolbox`. But I don't think that's the issue. From Java code, in order to execute commands as root, you will need to be authorized through the `su` binary. See [this question](http://stackoverflow.com/questions/6882248/running-shell-commands-though-java-code-on-android) for details. – mpontillo Feb 11 '13 at 22:35