Android 9(1),androidui库

本文详细介绍了如何使用反射调用来获取Android设备的内置存储和外置SD卡的根路径、判断外置SD卡是否挂载、获取手机内部及SD卡的剩余和总存储空间,以及系统总内存的方法。对于Android应用开发者来说,这些函数非常实用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

volume.setRemovable((boolean) volumeList[i].getClass().getMethod(“isRemovable”).invoke(volumeList[i]));

volume.setState((String) volumeList[i].getClass().getMethod(“getState”).invoke(volumeList[i]));

list_storagevolume.add(volume);

} catch (IllegalAccessException e) {

e.printStackTrace();

} catch (InvocationTargetException e) {

e.printStackTrace();

} catch (NoSuchMethodException e) {

e.printStackTrace();

}

}

} else {

LogUtils.D(“null-------------------------------------”);

}

} catch (Exception e1) {

e1.printStackTrace();

}

return list_storagevolume;

}

通过反射调用获取内置存储和外置sd卡根路径(通用)

/**

  • 通过反射调用获取内置存储和外置sd卡根路径(通用)

  • @param mContext 上下文

  • @param is_removale 是否可移除,false返回内部存储,true返回外置sd卡

  • @return

*/

private static String getStoragePath(Context mContext, boolean is_removale) {

StorageManager mStorageManager = (StorageManager) mContext.getSystemService(Context.STORAGE_SERVICE);

Class<?> storageVolumeClazz = null;

String path="";

try {

storageVolumeClazz = Class.forName(“android.os.storage.StorageVolume”);

Method getVolumeList = mStorageManager.getClass().getMethod(“getVolumeList”);

Method getPath = storageVolumeClazz.getMethod(“getPath”);

Method isRemovable = storageVolumeClazz.getMethod(“isRemovable”);

Object result = getVolumeList.invoke(mStorageManager);

final int length = Array.getLength(result);

for (int i = 0; i < length; i++) {

Object storageVolumeElement = Array.get(result, i);

path = (String) getPath.invoke(storageVolumeElement);

boolean removable = (Boolean) isRemovable.invoke(storageVolumeElement);

if (is_removale == removable) {

return path;

}

}

} catch (ClassNotFoundException e) {

e.printStackTrace();

} catch (InvocationTargetException e) {

e.printStackTrace();

} catch (NoSuchMethodException e) {

e.printStackTrace();

} catch (IllegalAccessException e) {

e.printStackTrace();

}

return path;

}

判断外置sd卡是否挂载

/**

  • 判断外置sd卡是否挂载

*/

public static boolean isStorageMounted(Context mContext) {

boolean isMounted = false;

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 getState = storageVolumeClazz.getMethod(“getState”);

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 state = (String) getState.invoke(storageVolumeElement);

if (removable && state.equals(Environment.MEDIA_MOUNTED)) {

isMounted =removable ;

break;

}

}

} catch (ClassNotFoundException e) {

e.printStackTrace();

} catch (InvocationTargetException e) {

e.printStackTrace();

} catch (NoSuchMethodException e) {

e.printStackTrace();

} catch (IllegalAccessException e) {

e.printStackTrace();

}

return isMounted;

}

获取手机内部剩余存储空间

/**

  • 获取手机内部剩余存储空间

  • @return

*/

public static long getAvailableInternalMemorySize() {

File path = Environment.getDataDirectory();

StatFs stat = new StatFs(path.getPath());

long blockSize = stat.getBlockSize();

long availableBlocks = stat.getAvailableBlocks();

return availableBlocks * blockSize;

}

获取手机内部总的存储空间

/**

  • 获取手机内部总的存储空间

  • @return

*/

public static long getTotalInternalMemorySize() {

File path = Environment.getDataDirectory();

StatFs stat = new S
tatFs(path.getPath());

long blockSize = stat.getBlockSize();

long totalBlocks = stat.getBlockCount();

return totalBlocks * blockSize;

}

获取SDCARD剩余存储空间

/**

  • 获取SDCARD剩余存储空间

  • @return

*/

public static long getAvailableExternalMemorySize() {

if (externalMemoryAvailable()) {

// File path = Environment.getExternalStorageDirectory();

// StatFs stat = new StatFs(path.getPath());

StatFs stat = new StatFs(getStoragePath(context,true));//path.getPath()

long blockSize = stat.getBlockSize();

long availableBlocks = stat.getAvailableBlocks();

return availableBlocks * blockSize;

} else {

return ERROR;

}

}

获取SDCARD总的存储空间

/**

  • 获取SDCARD总的存储空间

  • @return

*/

public static long getTotalExternalMemorySize() {

if (externalMemoryAvailable()) {

// File path = Environment.getExternalStorageDirectory();

// StatFs stat = new StatFs(path.getPath());

StatFs stat = new StatFs(getStoragePath(context,true));//path.getPath()

long blockSize = stat.getBlockSize();

long totalBlocks = stat.getBlockCount();

return totalBlocks * blockSize;

} else {

return ERROR;

}

}

获取系统总内存

/**

  • 获取系统总内存

  • @param context 可传入应用程序上下文。

  • @return 总内存大单位为B。

*/

public static long getTotalMemorySize(Context context) {

String dir = “/proc/meminfo”;

try {

FileReader fr = new FileReader(dir);

BufferedReader br = new BufferedReader(fr, 2048);

String memoryLine = br.readLine();

String subMemoryLine = memoryLine.substring(memoryLine.indexOf(“MemTotal:”));

br.close();

return Integer.parseInt(subMemoryLine.replaceAll("\D+", “”)) * 1024l;

} catch (IOException e) {

e.printStackTrace();

}

return 0;

}

ng dir = “/proc/meminfo”;

try {

FileReader fr = new FileReader(dir);

BufferedReader br = new BufferedReader(fr, 2048);

String memoryLine = br.readLine();

String subMemoryLine = memoryLine.substring(memoryLine.indexOf(“MemTotal:”));

br.close();

return Integer.parseInt(subMemoryLine.replaceAll("\D+", “”)) * 1024l;

} catch (IOException e) {

e.printStackTrace();

}

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值