SdCardUtils
import android.os.Environment; import android.os.StatFs; public class SdCardUtils { public static boolean hasSdcard() { String state = Environment.getExternalStorageState(); if (state.equals(Environment.MEDIA_MOUNTED)) { return true; } else { return false; } } /** * 计算sdcard上的剩余空间 * * @return */ public int freeSpaceOnSd() { StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath()); // (availableBlocks * blockSize)/1024 KIB 单位 // (availableBlocks * blockSize)/1024 /1024 MIB单位 double sdFreeMB = ((double) stat.getAvailableBlocks() * (double) stat.getBlockSize()) / (1024 * 1024); return (int) sdFreeMB; } }
最后,关注【码上加油站】微信公众号后,有疑惑有问题想加油的小伙伴可以码上加入社群,让我们一起码上加油吧!!!