【问题标题】:Android: Read-only storage behaves like writableAndroid:只读存储的行为类似于可写
【发布时间】:2023-03-07 10:06:01
【问题描述】:

我有一个应用程序可以测试所有存储是否可访问。您可能知道,Google 开始限制对外部存储的访问。因此,Kitkat 设备可以具有只读存储,这些存储只能对应用程序特定目录 (/Android/data/...) 具有可写访问权限。

我通过测试测试所有存储的路径是否可写:

if(new File("/storage_root_path/").canWrite() && new File("/storage_root_path/AppDir/").canWrite())
{
  //storage is writable
}
else
{
  //storage is readonly
}

/AppDir/ 是我的应用程序在存储根目录上的目录。这些测试适用于大多数设备。但我有一位用户使用三星 Galaxy S4 SGH-M919 (Kitkat),两项测试在他的外部 sd 卡上都返回 true。但存储不可写。

还有其他方法可以确定 Kitkat 上的存储是只读的吗?我可以尝试创建文件夹,但我希望有更好更快的解决方案。

感谢您的建议!

【问题讨论】:

    标签: android storage readonly android-4.4-kitkat


    【解决方案1】:

    确认。真的应该检查文件的可写性,File.canWrite() 没有帮助。我正在使用这种功能:

    private boolean isFileWritable(File file) {
        try {
            RandomAccessFile raf = new RandomAccessFile(file, "rw");
            FileChannel fc=raf.getChannel();
            FileLock fl=fc.lock();
            fl.release();
            fc.close();
            raf.close();
            return true;
        }
        catch(Exception ex) {
            return false;
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2013-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多