/** 获取存储卡路径 */
File sdcardDir=Environment.getExternalStorageDirectory();
/** StatFs 看文件系统空间使用情况 */
StatFs statFs=new StatFs(sdcardDir.getPath());
/** Block 的 size*/
Long blockSize=statFs.getBlockSize();
/** 总 Block 数量 */
Long totalBlocks=statFs.getBlockCount();
/** 已使用的 Block 数量 */
Long availableBlocks=statFs.getAvailableBlocks();
<!-- end header -->
java.lang.Object | |
↳ | android.os.Environment |
Class Overview(类概述)
Provides access to environment variables. 提供访问环境变量。
常量
Constants | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
String | MEDIA_BAD_REMOVAL | getExternalStorageState() returns MEDIA_BAD_REMOVAL if the media was removed before it was unmounted. | |||||||||
String | MEDIA_CHECKING | getExternalStorageState() returns MEDIA_CHECKING if the media is present and being disk-checked | |||||||||
String | MEDIA_MOUNTED | getExternalStorageState() returns MEDIA_MOUNTED if the media is present and mounted at its mount point with read/write access. | |||||||||
String | MEDIA_MOUNTED_READ_ONLY | getExternalStorageState() returns MEDIA_MOUNTED_READ_ONLY if the media is present and mounted at its mount point with read only access. | |||||||||
String | MEDIA_NOFS | getExternalStorageState() returns MEDIA_NOFS if the media is present but is blank or is using an unsupported filesystem | |||||||||
String | MEDIA_REMOVED | getExternalStorageState() returns MEDIA_REMOVED if the media is not present. | |||||||||
String | MEDIA_SHARED | getExternalStorageState() returns MEDIA_SHARED if the media is present not mounted, and shared via USB mass storage. | |||||||||
String | MEDIA_UNMOUNTABLE | getExternalStorageState() returns MEDIA_UNMOUNTABLE if the media is present but cannot be mounted. | |||||||||
String | MEDIA_UNMOUNTED | getExternalStorageState() returns MEDIA_UNMOUNTED if the media is present but not mounted. |
Public Constructors(构造函数) | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Environment() |
<!-- ========== METHOD SUMMARY =========== -->
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
static File | getDataDirectory() : Gets the Android data directory.获取Android的数据目录。 | ||||||||||
static File | getDownloadCacheDirectory() : Gets the Android Download/Cache content directory.获取的Android下载/缓存内容的目录。 | ||||||||||
static File | getExternalStorageDirectory() :Gets the Android external storage directory.获取Android的外部存储目录。 | ||||||||||
static File | getExternalStoragePublicDirectory(String type) :Get a top-level public external storage directory for placing files of a particular type.获取为特定类型的文件放在顶层的公共外部存储目录。 | ||||||||||
static String | getExternalStorageState() :Gets the current state of the primary "external" storage device.获取的主要“外部”存储设备的当前状态。 | ||||||||||
static File | getRootDirectory() :Gets the Android root directory.获取Android的根目录。 | ||||||||||
static boolean | isExternalStorageEmulated() :Returns whether the device has an external storage device which is emulated.返回设备是否有外部存储设备的仿真。 | ||||||||||
static boolean | isExternalStorageRemovable() :Returns whether the primary "external" storage device is removable.返回主“外部存储设备是否是可拆卸的。 |
public static File getExternalStorageDirectory ()
获取Android的外部存储目录。这个目录可能目前无法访问,如果它已被安装在其计算机上的用户,已被删除从设备,或发生其他一些问题。你可以确定其当前状态 getExternalStorageState()
。
注意:不要被这里的“外部”字混淆。这个目录可以更好地被看作媒体/共享存储。这是一个文件系统,可容纳的数据量相对较大,跨所有应用程序共享(不执行的权限)。传统上,这是一个SD卡,但它也可作为内置存储设备,从受保护的内部存储是不同的,可以在计算机上的文件系统安装在实施。
在与多个“外部”存储目录(如既安全应用程序存储和挂载共享存储)设备中,这个目录表示“小学化”的用户将与外部存储。
应用程序不应该直接使用这个顶级目录,以避免污染用户的根命名空间。任何专用应用程序的文件,这些文件应该被放置在返回目录Context.getExternalFilesDir
,该系统将采取删除,如果应用程序被卸载。其他共享文件应该被放置在一个由返回目录getExternalStoragePublicDirectory(字符串)
。
下面是一个典型的代码,例如监察外部存储的状态:
BroadcastReceiver mExternalStorageReceiver;
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
void updateExternalStorageState() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
mExternalStorageAvailable = mExternalStorageWriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
} else {
mExternalStorageAvailable = mExternalStorageWriteable = false;
}
handleExternalStorageState(mExternalStorageAvailable,
mExternalStorageWriteable);
}
void startWatchingExternalStorage() {
mExternalStorageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("test", "Storage: " + intent.getData());
updateExternalStorageState();
}
};
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_MEDIA_MOUNTED);
filter.addAction(Intent.ACTION_MEDIA_REMOVED);
registerReceiver(mExternalStorageReceiver, filter);
updateExternalStorageState();
}
void stopWatchingExternalStorage() {
unregisterReceiver(mExternalStorageReceiver);
}