我使用以下代码来检查SD卡是否可用,但是在运行Android 4.x的Samsung galaxy II和Note设备上以及稍后操作系统模拟具有SD卡,即使实际上设备上没有SD卡也是如此.
private boolean isSDCardAvailable() {
return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
}
如果SD卡可用,则调用getExternalCacheDir()获取我向用户显示错误的目录,并调用getCacheDir()以使用内部存储.
即使没有SD卡,上述方法也会返回TRUE和ALL设备.
解决方法:
自API级别11开始,此方法可用,它可以让您了解内部存储器是否可用作外部存储器.在某些设备中,即使SD卡不可用,getExternalStorageDirectory()也会返回true.阅读以下文档了解详情.
public static boolean isExternalStorageEmulated ()
Added in API level 11
Returns whether the device has an external storage device which is emulated. If true, the device does not have real external storage, and the directory returned by getExternalStorageDirectory() will be allocated using a portion of the internal storage system.
某些系统服务(例如程序包管理器)使用它来确定应用程序的安装位置.
Emulated external storage may also be encrypted – see setStorageEncryption(android.content.ComponentName, boolean) for additional details.