How do we copy files and retain their read only attribute in Android?

I don't believe the SD card supports file attributes like this. I think you can only make files read-only on the internal storage. (Because the SD card uses a FAT file system, but the internal storage uses a proper Linux filesystem.

).

Up vote 3 down vote favorite 1 share g+ share fb share tw.

In my Android app, I would like to be able to copy a read only file and have the new version also read only. Using the setReadOnly() method on the destination file only returns false, showing that it failed, and of course checking the file itself shows that it has not had the read only attribute set. --edit-- As David Given suggests below, this only applies to files on the SD card.

Internal files will behave appropriately with setReadOnly(). My manifest file has the line and I'm able to do all sorts of other file operations successfully in my app. In looking for a solution I've seen a couple of people using linux commands, and they've said that this is a 'dirty' way of doing it, so is there an alternative 'clean' way available to us?

The app I'm writing is using Android 2.1 if that's relevant. Java android file permissions android-2.1 link|improve this question edited Mar 3 at 22:07 asked Feb 25 at 10:08Phil Walker436.

I don't believe the SD card supports file attributes like this. I think you can only make files read-only on the internal storage. (Because the SD card uses a FAT file system, but the internal storage uses a proper Linux filesystem.

) Check the result of setReadOnly() and see whether it's returning true or false.

I have just checked again and I was trying to do this only on the SD card on the emulator. Using a real device I've successfully used setReadOnly() on a file copied to the /mnt folder (which is read only on the emu). Thanks.

– Phil Walker Mar 3 at 16:58 I'll try zxswing's suggestion to see if that works with the SD card. If not I'll mark your answer as correct. – Phil Walker Mar 3 at 22:11.

You can use Java Reflection to invoke FileUtils api. FileUtils. SetPermissions(f.getAbsolutePath(), FileUtils.

S_IRUSR | FileUtils. S_IWUSR | FileUtils. S_IRGRP | FileUtils.

S_IROTH, -1, -1) ; For Example: Class fileUtilsClass=Class. ForName("android.os. FileUtils"); Method method=fileUtilsClass.

GetMethod(...) method. Invoke(...).

I'm away from the code over the weekend, but I will see if this allows me to set file permissions on the SD card, if so I'll mark it as correct. Thanks for your help. – Phil Walker Mar 3 at 17:03 Sorry, this doesn't seem to work.

– Phil Walker Mar 7 at 11:20.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions