试图创建一个只读文件在Android上SD卡失败 [英] Attempt to create an readonly file on the android SD card fails

查看:220
本文介绍了试图创建一个只读文件在Android上SD卡失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个只读文件到我的SD卡。我需要做的这是一个两步的过程。

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(); 
}

其中方法1被调用,然后在后续阶段方法2.
cachedFileOnDisk.setReadOnly()调用返回一个错误给我,我无法设置文件为只读。

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.

有没有一种方法,我可以只读文件写入我的SD卡?

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

推荐答案

该SD卡采用FAT型文件系统不支持文件的所有权。

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

有可能在执行层面的FAT支持只读位,但是这将是有限的价值,因为任何其他与SD卡写入权限就可以自由改变位。鉴于有限的价值,这是一个开放性的问题,如果它来设置对Android的甚至有可能。

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.

这篇关于试图创建一个只读文件在Android上SD卡失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆