android获取外部存储资源,Android 7.0开发获取存储设备信息的方法

本文实例讲述了 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 mMountPathList = new CopyOnWriteArrayList();

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程序设计有所帮助。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值