今天爲大家分享下關於內存的事兒
可能很久之前,我們的獲取是否有 外置內存是這樣的(是否有sd卡以及sd卡相關內存信息)?
public static StatFs getSDMemory(Context context) {
StatFs statfs = null;
//判斷是否有插入存儲卡
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
//取得sdcard文件路徑
File path = Environment.getExternalStorageDirectory();
statfs = new StatFs(path.getPath());
//獲取block的SIZE
}
return statfs;
}
由上面可知,(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) 這個即可獲取是否有sd卡 。
但是到現在,毫無反應了 有木有!!!
變成了這樣 :
public static String getExtendedMemoryPath(Context mContext) {
StorageManager mStorageManager = (StorageManager) mContext.getSystemService(Context
.STORAGE_SERVICE);
Class<?> storageVolumeClazz = null;
try {
storageVolumeClazz = Class.forName("android.os.storage.StorageVolume");
Method getVolumeList = mStorageManager.getClass().getMethod("getVolumeList");
Method getPath = storageVolumeClazz.getMethod("getPath");
Method isRemovable = storageVolumeClazz.getMethod("isRemovable");
Method getDescription = storageVolumeClazz.getMethod("getDescription", Context.class);
Object result = getVolumeList.invoke(mStorageManager);
final int length = Array.getLength(result);
for (int i = 0; i < length; i++) {
Object storageVolumeElement = Array.get(result, i);
String path = (String) getPath.invoke(storageVolumeElement);
boolean removable = (Boolean) isRemovable.invoke(storageVolumeElement);
String descprition = (String) getDescription.invoke(storageVolumeElement, mContext);
if (removable && descprition.contains("SD")) {
return path;
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
好了。差不多就是樣子了。通過反射去拿這個isRemovable值,這個值代表是否有sd卡了。