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

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);

}

};

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/267626.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

A1032. 画三角形2

问题描述 找出下面给出图形的规律&#xff0c;给出n&#xff0c;画一个n行的对应图形。ABABCBABCDCBABCD输入格式输入包含一个数n。1<n<20输出格式输出与上图类似n行的图形。样例输入4样例输出ABABCBABCDCBABCDpackage www.tsinsen.com;import java.util.Scanner;public …

分析java中文乱码的原因

在java开发中都能遇到java中文乱码的情况&#xff0c;怎样才能够恰当地选择汉字编码方式并正确地处理汉字的编码呢?希望通过下面的总结的java中文乱码解决方法对遇到过此类问题的朋友有所帮助。 首先&#xff0c;要想解决java中文乱码问题就有必要了解一下什么是字符&#xff…

html中写随机数,为HTML生成一个随机数

你的问题是相当含糊&#xff0c;你需要什么&#xff0c;但这里是产生两个变量之间的随机数&#xff0c;然后一个Javascript的解决方案设置一个内容是&#xff1a;JS&#xff1a;var link document.getElementById(getNumber); // Gets the linklink.onclick getNumber; // Ru…

51Nod1469 淋漓尽致子串

首先&#xff0c;我们来定义一下淋漓尽致子串。 1.令原串为S。2.设子串的长度为len&#xff0c;在原串S中出现的次数为k&#xff0c;令其出现的位置为p1&#xff0c; p2&#xff0c; ....pk(即这个子串在原串中[pi&#xff0c;pi len - 1]中出现)。 3.若k1&#xff0c;则该子串…

Android运行Socket项目时出现错误 Error: ShouldNotReachHere()

在Android项目中实现Socket通信&#xff0c;服务器端使用main方法创建ServerSocket&#xff0c;运行启动服务器时报错“Error: ShouldNotReachHere() ”。 原因分析&#xff1a;java中使用main函数作为应用程序的接口&#xff0c;class的生命周期始于main方法&#xff0c;终于m…

腾讯测试鸿蒙系统,爆料:荣耀 30 Pro已开始测试华为鸿蒙系统

某数码博主今日放出了一张华为内部关于荣耀 30 Pro 测试 HarmonyOS 的截图&#xff0c;图片显示该机正运行基于 HarmonyOS 2.0 开发者测试版的系统。此外&#xff0c;他还透露荣耀 30 系列、V30 系列、Play4 Pro 下个月将升级到华为鸿蒙系统。华为在 2019 年开发者大会上正式推…

程序员跳槽全攻略pdf

下载地址&#xff1a;网盘下载和那些职场鸡汤不同&#xff0c;本书从价值论开始&#xff0c;引入职业画布&#xff0c;从九大方面为你讲解&#xff1b;有分析数据、有简历模板、有书写工具、有技能树图&#xff0c;堪称一本公司老板和HR最害怕你看到的跳槽百科。作者Easy为互联…

数字阅读体验的平台距离我们还有多远?

随着互联网的兴起&#xff0c;越来越多原本基于传统载体的信息内容也正在发生巨大的转变&#xff0c;当电子书和智能手机等载体开始承担阅读方式的时候&#xff0c;数字阅读的时代也正在全面来临。从近年来逐渐兴起的各种终端设备载体的发展历程来看&#xff0c;这个时代的来临…

使用MapReduce将HDFS数据导入Mysql

使用MapReduce将Mysql数据导入HDFS代码链接 将HDFS数据导入Mysql,代码示例 package com.zhen.mysqlToHDFS;import java.io.DataInput; import java.io.DataOutput; import java.io.IOException; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sq…

html多行文本框下拉,html基础-表单控件、密码框、单选按钮、复选框、多行文本框、下拉列表、按钮(提交、图片、重置)...

表单的介绍(将前端页面表单的值发送给后台&#xff0c;后台通过表单中name属性取值)可以获取客户端的信息(数据)&#xff0c;表单有各种各样的控件&#xff0c;输入框&#xff0c;复选框 按钮等表单的功能&#xff1a;交互功能表单的工作原理&#xff1a;浏览有表单的页面&…

Lync Server 2010的部署系列_第七章 部署边缘服务器(上)

一、配置边缘支持的内部DNS记录 1) 登录DC.Gianthard.com&#xff08;192.168.1.11&#xff09;。在相应的 DNS 服务器上&#xff0c;依次单击“开始”、“控制面板”、“管理工具”&#xff0c;然后单击“DNS”。 2) 在 SIP 域的控制台树中&#xff0c;展开“正向查找区域”&a…

iOS扩大按钮的点击范围

// 重写此方法将按钮的点击范围扩大 - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {CGRect bounds self.bounds;// 扩大点击区域bounds CGRectInset(bounds, -20, -20);// 若点击的点在新的bounds里面。就返回yesreturn CGRectContainsPoint(bounds, poin…

html5 txt文件上传,JavaScript html5利用FileReader实现上传功能

本文实例为大家分享了H5利用FileReader上传文件的具体代码&#xff0c;供大家参考&#xff0c;具体内容如下1. Html部分文件上传演练Browse...2. JS部分var result document.getElementById("result");var input document.getElementById("file_input");…

判断标签是否出界,重新设置样式

//样式重置&#xff0c;因为会获取到上次设置的样式 $(#releaseData).css({ "height": "auto", "bottom": "auto" }); //获取底部位置 var bottom$("#releaseData").css("bottom"); if (bottom.toString().indexO…

一起谈.NET技术,ASP.NET 请求处理流程

HTTP处理流程图 以上流程的一些概念解释&#xff1a; 1.http.sys 是一个位于Win2003和WinXP SP2中的操作系统核心组件&#xff0c;能够让任何应用程序通过它提供的接口&#xff0c;以http协议进行信息通讯。 温馨提示&#xff1a;如果用户不慎删除了该驱动文件&#xff0c;不用…

链接在HTML的英文,英文:A链接标记ie下会自动补全href_HTML/Xhtml_网页制作

英文:A链接标记ie下会自动补全href.Whilst working on the Ajax Link Tracker and MapSurface I have come across an inconsistency in how the href attribute is retrieved using DOM Scripting.The href attribute is different to other element attributes in that the v…

python实现文件加密

前言&#xff1a; 想实现批量文件加密&#xff0c;可惜批量。展时没有思路 0x1 没有加密前的图片 加密后&#xff01;&#xff01;&#xff01; &#xff01;&#xff01;&#xff01;打不开了 0x02: 代码 import hashlibdef get_sha1(f):xdopen(E:/1.txt,rb).read() #以读二进…

教徒计划出品:升级ESXI41-ESXI5

这个文档是教徒计划“教徒第一期”学员做的升级ESX41到ESXI5的资料&#xff0c;以后教徒计划学员做的资料共享时&#xff0c;我都会打上“教徒计划出品”字样&#xff0c;这样有别于“现任明教教主”出品。这样能够确认是谁主导做的这个事情。“教徒计划”这个平台能够给安全CC…

html是以一种通用的方法来,c++ 有一种通用的方法来使函数模板适应为多态函数对象吗?...

我有一些功能模板,例如template void foo(T);template void bar(T);// others我需要将每一个传递给一种算法,它将被称为各种类型的算法.template void some_algorithm(F f){// call f with argument of type int// call f with argument of type SomeClass// etc.}我不能传递我…

百度2011大会见闻:百度开始推出耀主页

9月2号是百度大会的日子&#xff0c;之前通过51CTO注册&#xff0c;获得了参会资格&#xff0c;感谢51CTO带来的机会&#xff0c;可以有幸到现场观看到百度总裁李彦宏的精彩演讲。<?xml:namespace prefix o ns "urn:schemas-microsoft-com:office:office" />…