首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

获取SdCard途径及剩余容量

2012-08-27 
获取SdCard路径及剩余容量?/**?? ? ? ? ?* 获取SdCard路径?? ? ? ? ?*/?? ? ? ? public static String get

获取SdCard路径及剩余容量

?/**

?? ? ? ? ?* 获取SdCard路径

?? ? ? ? ?*/

?? ? ? ? public static String getExternalStoragePath() {

?? ? ? ? ? ? ? ? ? // 获取SdCard状态

?? ? ? ? ? ? ? ? ? String state = android.os.Environment.getExternalStorageState();

?? ? ? ? ? ? ? ? ? // 判断SdCard是否存在并且是可用的

?? ? ? ? ? ? ? ? ? if (android.os.Environment.MEDIA_MOUNTED.equals(state)) {

?? ? ? ? ? ? ? ? ? ? ? ? ? ?if (android.os.Environment.getExternalStorageDirectory().canWrite()) {

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? return android.os.Environment.getExternalStorageDirectory().getPath();

?? ? ? ? ? ? ? ? ? ? ? ? ? ?}

?? ? ? ? ? ? ? ? ? }

?? ? ? ? ? ? ? ? ? return null;

?? ? ? ? }

?

?? ? ? ? /**

?? ? ? ? ?* 获取存储卡的剩余容量,单位为字节

?? ? ? ? ?* @param filePath

?? ? ? ? ?* @return availableSpare

?? ? ? ? ?*/

?? ? ? ? public static long getAvailableStore(String filePath) {

?? ? ? ? ? ? ? ? ? // 取得sdcard文件路径

?? ? ? ? ? ? ? ? ? StatFs statFs = new StatFs(filePath);

?? ? ? ?// 获取block的SIZE

?? ? ? ?long blocSize = statFs.getBlockSize();

?? ? ? ?// 获取BLOCK数量

?? ? ? ?long totalBlocks = statFs.getBlockCount();

?? ? ? ?// 可使用的Block的数量

?? ? ? ?long availaBlock = statFs.getAvailableBlocks();

?

?? ? ? ?long total = totalBlocks * blocSize;

?

?? ? ? ?long availableSpare = availaBlock * blocSize;

?

?? ? ? ?return availableSpare;

?? ? ? ? }

热点排行