Android 7.0开发获取存储设备信息的方法
作者:王大锤扛刀 时间:2022-05-24 01:04:57
本文实例讲述了 Android 7.0开发获取存储设备信息的方法。分享给大家供大家参考,具体如下:
Android 7.0开发相较之前有不少改进,具体可参考前面的文章Android7.0版本影响开发的改进分析,这里简单总结一下Android 7.0针对存储设备的简单操作方法。
MountPoint
我们通过MountPoint来描述android设备信息
private static class MountPoint {
String mDescription;
String mPath;
boolean mIsExternal;
boolean mIsMounted;
long mMaxFileSize;
long mFreeSpace;
long mTotalSpace;
}
实现mMountPathList
private final CopyOnWriteArrayList <MountPoint> mMountPathList = new CopyOnWriteArrayList<MountPoint>();
public void init(Context context) {
mStorageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
final String defaultPath = getDefaultPath();
LogUtils.d(TAG, "init,defaultPath = " + defaultPath);
if (!TextUtils.isEmpty(defaultPath)) {
mRootPath = ROOT_PATH;
}
mMountPathList.clear();
// check media availability to init mMountPathList
StorageVolume[] storageVolumeList = mStorageManager.getVolumeList();
if (storageVolumeList != null) {
for (StorageVolume volume : storageVolumeList) {
MountPoint mountPoint = new MountPoint();
mountPoint.mDescription = volume.getDescription(context);
mountPoint.mPath = volume.getPath();
mountPoint.mIsMounted = isMounted(volume.getPath());
mountPoint.mIsExternal = volume.isRemovable();
mountPoint.mMaxFileSize = volume.getMaxFileSize();
LogUtils.d(TAG, "init,description :" + mountPoint.mDescription + ",path : "
+ mountPoint.mPath + ",isMounted : " + mountPoint.mIsMounted
+ ",isExternal : " + mountPoint.mIsExternal + ", mMaxFileSize: " + mountPoint.mMaxFileSize);
mMountPathList.add(mountPoint);
}
}
IconManager.getInstance().init(context, defaultPath + SEPARATOR);
}
判断是否是外置sdcard
/**
* This method checks weather certain path is external mount path.
*
* @param path path which needs to be checked
* @return true for external mount path, and false for not external mount path
*/
public boolean isExternalMountPath(String path) {
//LogUtils.d(TAG, "isExternalMountPath ,path =" + path);
if (path == null) {
return false;
}
for (MountPoint mountPoint : mMountPathList) {
if (mountPoint.mIsExternal && mountPoint.mPath.equals(path)) {
return true;
}
}
return false;
}
判断内置存储空间
public boolean isInternalMountPath(String path) {
//LogUtils.d(TAG, "isInternalMountPath ,path =" + path);
if (path == null) {
return false;
}
for (MountPoint mountPoint : mMountPathList) {
if (!mountPoint.mIsExternal && mountPoint.mPath.equals(path)) {
return true;
}
}
return false;
}
希望本文所述对大家Android程序设计有所帮助。
来源:http://blog.csdn.net/wangjicong_215/article/details/76573014
标签:Android7.0,存储设备
0
投稿
猜你喜欢
C#实现的Win32控制台线程计时器功能示例
2022-12-19 20:29:20
C#中析构函数、Dispose、Close方法的区别
2022-06-11 21:20:54
C#窗体传值代码方法
2022-12-03 22:57:37
Java中对list map根据map某个key值进行排序的方法
2023-09-04 17:10:03
Android完整Socket解决方案
2023-12-22 14:42:40
手把手带你了解Java-Stream流方法学习及总结
2023-11-25 19:30:15
Java之SpringBean生命周期问题理解
2022-11-16 14:47:35
如何在android中使用html作布局文件
2023-01-14 07:49:10
java代码执行字符串中的逻辑运算方法
2023-11-29 12:13:06
详解Java的文件与目录管理以及输入输出相关操作
2022-05-03 15:07:22
Java ByteBuffer网络编程用法实例解析
2022-09-17 20:16:22
Android app第三方支付宝支付接入教程
2022-06-05 20:02:19
简单捋捋@RequestParam 和 @RequestBody的使用
2022-11-02 16:19:20
Netty启动流程注册多路复用源码解析
2021-12-02 16:51:35
Android性能优化之ViewPagers + Fragment缓存优化
2022-08-13 10:23:35
Springboot公共字段填充及ThreadLocal模块改进方案
2023-11-17 22:58:39
安卓Android6.0权限动态获取操作示例
2023-01-26 22:56:43
C#中加载dll并调用其函数的实现方法
2022-06-27 17:14:08
android 照相功能的简单实例
2023-08-08 01:11:55
SpringBoot yaml中的数组类型取值方式
2022-10-14 20:05:36