Android:只读存储的行为类似于可写

时间:2014-04-25 09:49:14

标签: android storage readonly android-4.4-kitkat

我有一个应用程序,我测试所有存储是否可访问。您可能知道,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上的存储是只读的?我可以尝试创建文件夹,但我希望有更好更快的解决方案。

感谢您的任何建议!

1 个答案:

答案 0 :(得分:0)

确认。一个人应该真正检查文件的可写性,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;
    }

}