首先要在AndroidManifest.xml中增加SD卡访问权限
判断SD卡是否存在privateboolean ExistSDCard()
{ if (android.os.Environment.getExternalStorageState().
equals( android.os.Environment.MEDIA_MOUNTED)) {
returntrue; } elsereturnfalse; }
查看SD卡的剩余空间publiclong getSDFreeSize(
){ //取得SD卡文件路径 File path = Environment.
getExternalStorageDirectory(); StatFs sf = ne
w StatFs(path.getPath()); //获取单个数据块的大小(Byte)
long blockSize = sf.getBlockSize(); //空闲的数据块的数量
long freeBlocks = sf.getAvailableBlocks();
//返回SD卡空闲大小 //return freeBlocks * blockSize;
//单位Byte //return (freeBlocks * blockSize)/1024;
//单位KB return (freeBlocks * blockSize)/1024 /1024; //单位MB }
查看SD卡总容量publiclong getSDAllSize(){ //取得SD卡文件路径
File path = Environment.getExternalStorageDirectory();
StatFs sf = new StatFs(path.getPath()); //获取单个数据块的大小(Byte)
long blockSize = sf.getBlockSize(); //获取所有数据块数long allBlocks = sf.getBlockCount();
//返回SD卡大小 //return allBlocks * blockSize; //单位Byte //return (allBlocks * blockSize)/1024;
//单位KBreturn (allBlocks * blockSize)/1024/1024; //单位MB
判断SD卡是否存在privateboolean ExistSDCard()
{ if (android.os.Environment.getExternalStorageState().
equals( android.os.Environment.MEDIA_MOUNTED)) {
returntrue; } elsereturnfalse; }
查看SD卡的剩余空间publiclong getSDFreeSize(
){ //取得SD卡文件路径 File path = Environment.
getExternalStorageDirectory(); StatFs sf = ne
w StatFs(path.getPath()); //获取单个数据块的大小(Byte)
long blockSize = sf.getBlockSize(); //空闲的数据块的数量
long freeBlocks = sf.getAvailableBlocks();
//返回SD卡空闲大小 //return freeBlocks * blockSize;
//单位Byte //return (freeBlocks * blockSize)/1024;
//单位KB return (freeBlocks * blockSize)/1024 /1024; //单位MB }
查看SD卡总容量publiclong getSDAllSize(){ //取得SD卡文件路径
File path = Environment.getExternalStorageDirectory();
StatFs sf = new StatFs(path.getPath()); //获取单个数据块的大小(Byte)
long blockSize = sf.getBlockSize(); //获取所有数据块数long allBlocks = sf.getBlockCount();
//返回SD卡大小 //return allBlocks * blockSize; //单位Byte //return (allBlocks * blockSize)/1024;
//单位KBreturn (allBlocks * blockSize)/1024/1024; //单位MB