当前位置: 首页 > 工具软件 > V2EX-android > 使用案例 >

Android 9,2021年GitHub上那些优秀Android开源库总结

施超
2023-12-01

try {

volume = new Volume();

volume.setPath((String) volumeList[i].getClass().getMethod(“getPath”).invoke(volumeList[i]));

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

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

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享

umeList[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 StatFs(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;

}

获取当前可用内存

/**

  • 获取当前可用内存,返回数据以字节为单位。

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

  • @return 当前可用内存单位为B。

*/

public static long getAvailableMemory(Context context) {

ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);

ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();

am.getMemoryInfo(memoryInfo);

return memoryInfo.availMem;

}

private static DecimalFormat fileIntegerFormat = new DecimalFormat("#0");

private static DecimalFormat fileDecimalFormat = new DecimalFormat("#0.#");

单位换算

/**

  • 单位换算

  • @param size 单位为B

  • @param isInteger 是否返回取整的单位

  • @return 转换后的单位

*/

 类似资料: