user1454356
user1454356

Reputation: 1007

Attempt to create an readonly file on the android SD card fails

I am attempting to write a readonly file to my SD card. I need to do this as a two step process.

public void method1(String cacheFilename) {
File cacheDir = mContext.getExternalCacheDir();
File cachedFileOnDisk = new File(cacheDir, cacheFilename);
FileOutputStream fileStream = new FileOutputStream(cachedFile);
fileStream.write(...)
fileStream.flush();
fileStream.close();
}
public void method2(String cacheFilename) {
   File cacheDir = mContext.getExternalCacheDir();
   File cachedFileOnDisk = new File(cacheDir, cacheFilename);
   if(!cachedFileOnDisk.setReadOnly()) {
       throw new IllegalStateException(); 
}

Where method1 is invoked and then at a subsequent stage method2. The cachedFileOnDisk.setReadOnly() call returns a false to me and I am unable to set the file to read only.

Is there a way I could write readonly files to my SD card?

Upvotes: 3

Views: 703

Answers (1)

Chris Stratton
Chris Stratton

Reputation: 40347

The sdcard uses a FAT-type filesystem which does not support file ownership.

It is possible at implementation level for FAT to support a read-only bit, but that would be of limited value since anything else with sd card write permission would be free to change the bit. Given the limited value, it's an open question if it's even possible to set that on android.

Upvotes: 5

Related Questions