android – 正确检测SD卡是否可用

我使用以下代码来检查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.

Android docs

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...