Android 4.4 SD 和 U盘 的状态

通过获取StorageVolume 类来判断 是sd还是U盘。我们来看一下这个类

package android.os.storage;

import android.content.Context;

import android.os.Parcel;

import android.os.Parcelable;

import android.os.UserHandle;

import com.android.internal.util.IndentingPrintWriter;

import java.io.CharArrayWriter;

import java.io.File;

/**

* Description of a storage volume and its capabilities, including the

* filesystem path where it may be mounted.

*

* @hide

*/

public class StorageVolume implements Parcelable {

// TODO: switch to more durable token

private int mStorageId;

private final File mPath;

private final int mDescriptionId;

private final boolean mPrimary;

private final boolean mRemovable;

private final boolean mEmulated;

private final int mMtpReserveSpace;

private final boolean mAllowMassStorage;

/** Maximum file size for the storage, or zero for no limit */

private final long mMaxFileSize;

/** When set, indicates exclusive ownership of this volume */

private final UserHandle mOwner;

private String mUuid;

private String mUserLabel;

private String mState;

// StorageVolume extra for ACTION_MEDIA_REMOVED, ACTION_MEDIA_UNMOUNTED, ACTION_MEDIA_CHECKING,

// ACTION_MEDIA_NOFS, ACTION_MEDIA_MOUNTED, ACTION_MEDIA_SHARED, ACTION_MEDIA_UNSHARED,

// ACTION_MEDIA_BAD_REMOVAL, ACTION_MEDIA_UNMOUNTABLE and ACTION_MEDIA_EJECT broadcasts.

public static final String EXTRA_STORAGE_VOLUME = "storage_volume";

// About storage volume format fs type

public static final String EXTRA_FORMAT_FSTYPE = "format_fstype";

public StorageVolume(File path, int descriptionId, boolean primary, boolean removable,

boolean emulated, int mtpReserveSpace, boolean allowMassStorage, long maxFileSize,

UserHandle owner) {

mPath = path;

mDescriptionId = descriptionId;

mPrimary = primary;

mRemovable = removable;

mEmulated = emulated;

mMtpReserveSpace = mtpReserveSpace;

mAllowMassStorage = allowMassStorage;

mMaxFileSize = maxFileSize;

mOwner = owner;

}

private StorageVolume(Parcel in) {

mStorageId = in.readInt();

mPath = new File(in.readString());

mDescriptionId = in.readInt();

mPrimary = in.readInt() != 0;

mRemovable = in.readInt() != 0;

mEmulated = in.readInt() != 0;

mMtpReserveSpace = in.readInt();

mAllowMassStorage = in.readInt() != 0;

mMaxFileSize = in.readLong();

mOwner = in.readParcelable(null);

mUuid = in.readString();

mUserLabel = in.readString();

mState = in.readString();

}

public static StorageVolume fromTemplate(StorageVolume template, File path, UserHandle owner) {

return new StorageVolume(path, template.mDescriptionId, template.mPrimary,

template.mRemovable, template.mEmulated, template.mMtpReserveSpace,

template.mAllowMassStorage, template.mMaxFileSize, owner);

}

/**

* Returns the mount path for the volume.

*

* @return the mount path

*/

public String getPath() {

return mPath.toString();

}

public File getPathFile() {

return mPath;

}

/**

* Returns a user visible description of the volume.

*

* @return the volume description

*/

public String getDescription(Context context) {

return context.getResources().getString(mDescriptionId);

}

public int getDescriptionId() {

return mDescriptionId;

}

public boolean isPrimary() {

return mPrimary;

}

/**

* Returns true if the volume is removable.

*

* @return is removable

*/

public boolean isRemovable() {

return mRemovable;

}

/**

* Returns true if the volume is emulated.

*

* @return is removable

*/

public boolean isEmulated() {

return mEmulated;

}

/**

* Returns the MTP storage ID for the volume.

* this is also used for the storage_id column in the media provider.

*

* @return MTP storage ID

*/

public int getStorageId() {

return mStorageId;

}

/**

* Do not call this unless you are MountService

*/

public void setStorageId(int index) {

// storage ID is 0x00010001 for primary storage,

// then 0x00020001, 0x00030001, etc. for secondary storages

mStorageId = ((index + 1) << 16) + 1;

}

/**

* Number of megabytes of space to leave unallocated by MTP.

* MTP will subtract this value from the free space it reports back

* to the host via GetStorageInfo, and will not allow new files to

* be added via MTP if there is less than this amount left free in the storage.

* If MTP has dedicated storage this value should be zero, but if MTP is

* sharing storage with the rest of the system, set this to a positive value

* to ensure that MTP activity does not result in the storage being

* too close to full.

*

* @return MTP reserve space

*/

public int getMtpReserveSpace() {

return mMtpReserveSpace;

}

/**

* Returns true if this volume can be shared via USB mass storage.

*

* @return whether mass storage is allowed

*/

public boolean allowMassStorage() {

return mAllowMassStorage;

}

/**

* Returns maximum file size for the volume, or zero if it is unbounded.

*

* @return maximum file size

*/

public long getMaxFileSize() {

return mMaxFileSize;

}

public UserHandle getOwner() {

return mOwner;

}

public void setUuid(String uuid) {

mUuid = uuid;

}

public String getUuid() {

return mUuid;

}

/**

* Parse and return volume UUID as FAT volume ID, or return -1 if unable to

* parse or UUID is unknown.

*/

public int getFatVolumeId() {

if (mUuid == null || mUuid.length() != 9) {

return -1;

}

try {

return Integer.parseInt(mUuid.replace("-", ""), 16);

} catch (NumberFormatException e) {

return -1;

}

}

public void setUserLabel(String userLabel) {

mUserLabel = userLabel;

}

public String getUserLabel() {

return mUserLabel;

}

public void setState(String state) {

mState = state;

}

public String getState() {

return mState;

}

@Override

public boolean equals(Object obj) {

if (obj instanceof StorageVolume && mPath != null) {

StorageVolume volume = (StorageVolume)obj;

return (mPath.equals(volume.mPath));

}

return false;

}

@Override

public int hashCode() {

return mPath.hashCode();

}

@Override

public String toString() {

final CharArrayWriter writer = new CharArrayWriter();

dump(new IndentingPrintWriter(writer, " ", 80));

return writer.toString();

}

public void dump(IndentingPrintWriter pw) {

pw.println("StorageVolume:");

pw.increaseIndent();

pw.printPair("mStorageId", mStorageId);

pw.printPair("mPath", mPath);

pw.printPair("mDescriptionId", mDescriptionId);

pw.printPair("mPrimary", mPrimary);

pw.printPair("mRemovable", mRemovable);

pw.printPair("mEmulated", mEmulated);

pw.printPair("mMtpReserveSpace", mMtpReserveSpace);

pw.printPair("mAllowMassStorage", mAllowMassStorage);

pw.printPair("mMaxFileSize", mMaxFileSize);

pw.printPair("mOwner", mOwner);

pw.printPair("mUuid", mUuid);

pw.printPair("mUserLabel", mUserLabel);

pw.printPair("mState", mState);

pw.decreaseIndent();

}

public static final Creator CREATOR = new Creator() {

@Override

public StorageVolume createFromParcel(Parcel in) {

return new StorageVolume(in);

}

@Override

public StorageVolume[] newArray(int size) {

return new StorageVolume[size];

}

};

@Override

public int describeContents() {

return 0;

}

@Override

public void writeToParcel(Parcel parcel, int flags) {

parcel.writeInt(mStorageId);

parcel.writeString(mPath.toString());

parcel.writeInt(mDescriptionId);

parcel.writeInt(mPrimary ? 1 : 0);

parcel.writeInt(mRemovable ? 1 : 0);

parcel.writeInt(mEmulated ? 1 : 0);

parcel.writeInt(mMtpReserveSpace);

parcel.writeInt(mAllowMassStorage ? 1 : 0);

parcel.writeLong(mMaxFileSize);

parcel.writeParcelable(mOwner, flags);

parcel.writeString(mUuid);

parcel.writeString(mUserLabel);

parcel.writeString(mState);

}

}

这个类用来描述存储状态,有几个重要的参数:

File mPath 路径

mRemovable 是否可移除,(U盘,SD)返回 true

getDescription(context) 获取设备的描述 "sd" 就是SD卡,“USB”就是usb设备

获取存储设备

mStorageManager = StorageManager.from(this);

final StorageVolume[] storageVolumes = mStorageManager.getVolumeList();

for (StorageVolume mVolume : storageVolumes) {

if(mVolume != null){

String description = mVolume.getDescription(context);

Log.d(getClass().getName(), "description is: " + description);

if(description != null && (description.contains("SD") || description.contains("sd"))){

//这是sd

}

else if(description != null && (description.contains("USB") || description.contains("usb")

|| description.contains("Usb"))){

//这是usb

}

}

}

}

存储设备状态和路径

import android.os.storage.StorageManager;

import android.os.storage.StorageEventListener;

private StorageManager mStorageManager;

mStorageManager = StorageManager.from(this);

//注册和反注册

mStorageManager.registerListener(mStorageListener);

if (mStorageManager != null && mStorageListener != null) {

mStorageManager.unregisterListener(mStorageListener);

}

/**

*

* 监听 path 路径 oldState 上一个状态 newState 当前状态

* 所有状态 mounted unmounted removed checking

*/

StorageEventListener mStorageListener = new StorageEventListener() {

@Override

public void onStorageStateChanged(String path, String oldState, String newState) {

Log.i("qkmin -voolesettings"," StorageEventListener Received storage state changed notification that " + path +

" changed state from " + oldState + " to " + newState);

}

};

android判断sd u盘,[Android Framework]获取U盘 SD 状态相关推荐

  1. android中通过java的反射机制获取U盘名称

    在android项目中实现Windos一样的U盘效果,需监听两个U盘的插入和拔出状态,并屏蔽本地存储,使用U盘来下载和上传文件: 在编辑代码中碰到一个问题:无法获取插入U盘的名称,于是通过java的反 ...

  2. android判断参数非空,Android Studio注释模板Live Templates参数获取不到为null的一些

    Android Studio注释模板Live Templates参数获取不到为null的一些 Android Studio注释模板Live Templates参数获取不到为null的一些解决方法 提示 ...

  3. c# 获取路径的盘符_c#获取驱动器盘符

    在编写某些Windows管理应用程序时,能够从自定义的驱动器选择列表框中进行选择无疑是非常专业的,使用Delphi或者C++的早期版本时我们都必须使用Win32所提供的许多关于获取磁盘信息的API函数 ...

  4. android storagemanager来获取u盘名称,StorageManager获取U盘挂载状态

    StorageManager是Android SDK中管理存储设备的一个类.其中的存储设备分内部存储和外部存储,外部存储可以有SDCard.U盘等其他挂载的外设. StorageVolume代表的是一 ...

  5. android 判断 音乐是否播放,Android如何判断当前手机是否正在播放音乐并获取有关正在播放的音乐的信息...

    我想实现以下情况,以确定音乐是否正在Android手机上播放,如果是,则通过特定手势进行播放, 或单击一个按钮分享我当前正在听的音乐. 第一步是确定当前是否正在播放音乐. 一开始,我认为这有点复杂. ...

  6. android 获取u盘名字_Android 获取U盘的卷标(支持中文卷标)

    [在学习Android开发的过程你,你往往会去借鉴别人的应用是怎么开发的,那些漂亮的动画和精致的布局可能会让你爱不释手,作为一个开发者,你可能会很想知道这些效果界面是怎 1: 盘符 和 卷标的区别 之 ...

  7. android 判断华为系统版本号,Android获取系统(ROM)类别及版本号

    很多时候我们需要知道用户当前使用的是什么系统,甚至是系统的版本号(比如MIUI V7.V8)来进一步处理业务逻辑,比如打开系统权限设置界面. 感谢国内各大Android手机/系统生产商,让我们这些An ...

  8. android 获取u盘名字_android 获取U盘路径

    private static final String MOUNTS_FILE = "/proc/mounts"; public static boolean isMounted( ...

  9. android判断是否json格式,Android判断json格式将错误信息提交给服务器

    开发中发现, 服务器偶尔会发送错误格式 json 给 Android 客户端, 导致 Android 客户端 json解析失败, 应用异常. 并非服务器有意坑客户端, 而是客户端请求服务器数据时, 除 ...

最新文章

  1. 最新《科学》重磅!科学家喊你赶快关心自己的孩子:缺乏母爱会导致大脑基因改变...
  2. Visual Studio 2008/2010中Xaml开发格式设置技巧
  3. VSCode工程文件右侧A M U 字母的含义
  4. react map循环生成的button_关于Vue和React的一些对比及个人思考(中)
  5. 单列集合Set的实现类HashSet
  6. java语言factory_一个简单例子解释 Java factory
  7. zendstudio for mac破解版
  8. 采用Mono进行移动开发图书推荐
  9. Creative Coding创意+技术的世界
  10. 使虚拟光驱DaemonTool在电脑开机时不自动启动
  11. 1279C. Stack of Presents
  12. 手机唯一标识IMEI以及与IMSI的区别
  13. Exercise – SE Technical Boot Camp Hands-on Exercises
  14. 解决IE系列浏览器上传页面接收问题
  15. 2020洪灾地图_2020的汛情有多严重?居然已经到了这种地步!
  16. 什么是Hibernate方言和方言列表
  17. HTML进阶(2)- 在页面中使用flash
  18. 日语在线学习网站简介
  19. 计算机分类汇总表格,Excel分类汇总使用全攻略
  20. 《Adobe Flash CS6中文版经典教程》——1.4 了解“时间轴”

热门文章

  1. 《算法之道》精华 经典算法部分
  2. [导入]FreeTextBox 1.6.3 中文版使用说明
  3. C#:向C++封送结构体数组
  4. Halcon:模版匹配
  5. nand flash坏块管理OOB,BBT,ECC
  6. Fedora15安装NVIDIA显卡驱动全过程
  7. halcon边缘检测的方法及各种方法的适用范围
  8. Machine Vision Pixel Calibration~ ~ ~ ~ ~ ~ ~ ~ ~ ~
  9. 在新的固态硬盘只装ubuntu16.04系统,重启后无启动项解决方案
  10. cvc 降噪_耳机降噪功能这么多,说说什么是ANC、ENC、CVC、DSP降噪