清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_main_layout); /**设备管理器*/ StorageManager storageManager = (StorageManager) getSystemService(Context.STORAGE_SERVICE); try { Class<?>[] paramClasses = {}; /**反射实现*/ Method getVolumePathsMethod = StorageManager.class.getMethod("getVolumePaths", paramClasses); getVolumePathsMethod.setAccessible(true); Object[] params = {}; Object invoke = getVolumePathsMethod.invoke(storageManager, params); for (int i = 0; i < ((String[]) invoke).length; i++) { /** Android.os下的StatFs类主要用来获取文件系统的状态,能够获取sd卡的大小和剩余空间,获取系统内部空间也就是/system的大小和剩余空间等等。*/ StatFs stat = getStatFs(((String[]) invoke)[i]); Log.i("ldm", ((String[]) invoke)[i] + ":" + calculateSizeInMB(stat)); } } catch (Exception e1) { e1.printStackTrace(); } } private StatFs getStatFs(String path) { try { return new StatFs(path); } catch (Exception e) { e.printStackTrace(); } return null; } /**计算存储卡的可用容量(单位:M)*/ private float calculateSizeInMB(StatFs stat) { if (stat != null) return stat.getAvailableBlocks() * (stat.getBlockSize() / (1024f * 1024f)); return 0.0f; } }