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
  • 4,081
  • 17
  • 75
  • 132

1 Answers1

1

Use caution with this approach. The SD card generally 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,559
  • 7
  • 62
  • 90
  • 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
  • First part is just wrong. There is no hard rule on the filesystem type on Android. But yes I wouldn't use `chmod` usually on Android either way. – xeruf Dec 22 '22 at 20:29
  • Updated the answer slightly to clarify. When I wrote this answer ~11 years ago, I worked with a wide variety of major Android devices, and none of them coped well with non-FAT formatted SD cards. While this may have changed, I feel like removable SD cards have also fallen out of fashion. – mpontillo Jan 11 '23 at 23:05