Android 推断SD卡是否存在及容量查询

与天地兮比寿,与日月兮齐光。这篇文章主要讲述Android 推断SD卡是否存在及容量查询相关的知识,希望能为你提供帮助。

首先先要加入权限 < uses-permission android :name =" android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> < uses-permission android: name=" android.permission.WRITE_EXTERNAL_STIRAGE" />




推断SD卡是否存在
【Android 推断SD卡是否存在及容量查询】
/* * 推断SD卡是否存在 */ private boolean ExitSDcard() { if (Environment.getExternalStorageState().equals( android.os.Environment.MEDIA_UNMOUNTED)) { return true; } else { return false; } }


< span style=" white-space:pre" > < /span> /* * 查看SD卡总容量 */ @SuppressWarnings(" deprecation" ) public long getSDAllSize() { String path = Environment.getExternalStorageDirectory().getPath(); StatFs sf = new StatFs(path); int blockSize = sf.getBlockSize(); int allBlocks = sf.getBlockCount(); return (allBlocks * blockSize) / 1024 / 1024; }

/* * * 查看SD卡剩余空间 */ @SuppressWarnings(" deprecation" ) public long getSDFreeSize() { String path = Environment.getExternalStorageDirectory().getPath(); StatFs statFs = new StatFs(path); int size = statFs.getBlockSize(); int freeBlocks = statFs.getAvailableBlocks(); return (freeBlocks * size) / 1024 / 1024; }












    推荐阅读