各位android的高手,请问怎么得到,判断一台机器上usb盘的目录是什么
或者怎么判断一个路径是不是usb盘的
一个实例如下(我现在的一个android设备现状):
1,有内部存储器sdcard,具体路径:/mnt/sdcard
2,有tf卡,具体路径:/mnt/ext_sd
3,有u盘,具体路径:/mnt/usbhost1
我可以得到/mnt/usbhost1,并且确定其是u盘的路径
但是其他设备不一定会和我这个设备相同,
比如有的设备u盘路径可能是/mnt/sda1,tf卡设备路径可能是/mnt/tfcard
我想知道有没有方法可以获取到/mnt/sda1,并且确定其是u盘的路径而不是tf卡路径
我现在是通过以下方法判断的
String devicePath = intent.getDataString().toLowerCase().substring(7);
if (devicePath.contains("usb"))
{
//这个是u盘
}
但是这并不准确,如上面的/mnt/sda1
Environment.getExternalStoragePublicDirectory()
Context.getExternalFilesDir()
public static String getExtSDCard(){
File[] files=new File("/mnt").listFiles();
String sdcard=Environment.getExternalStorageDirectory().getAbsolutePath().toLowerCase();
String file;
for (int i = 0; i < files.length; i++) {
file=files[i].getAbsolutePath().toLowerCase();
if(!file.equals(sdcard)&&(file.contains("ext")||file.contains("sdcard"))){
return file;
}
}
return null;
}
改进一下
你也可以遍历mnt路径下面的所有设备 使用
StatFs statFs=new StatFs(path);
来读取大小信息 大的自然就是存储卡了 然后再根据名字来排除内部sdcard即可获得外部存储卡路径
public static String getExtSDCard(){
File[] files=new File("/mnt").listFiles();
String sdcard=Environment.getExternalStorageDirectory().getAbsolutePath();
String file;
for (int i = 0; i < files.length; i++) {
file=files[i].getAbsolutePath();
if(!file.equalsIgnoreCase(sdcard)&&file.contains("ext")){
return file;
}
}
return null;
}
你可以这么做 根据经验所有sdcard都在mnt下面 首先获取默认sdcard的路径 然后遍历mnt下面的所有设备 如果出了默认sdcard之外还有别的设备名字包含"ext"字符的 则返回该设备名字(极大可能是扩展sdcard)
各个厂商不一样,设备节点命名的方式不一样,挂载的路径也不一样
可以通过mount命令来分析
之前做过一个项目有个需求是将外接存储设备细分:USB连接和TF卡等存储卡两类。当时网上找的资料都是要判断外接存储设备的状态只能监听android.intent.action.MEDIA_MOUNTED
等一系列的action;但系统只提供一个监听,怎么办?你无法判断监听到的是哪一类存储设备。经过思考发现两类存储在系统的路径是不同的可以根据路径是否存在来进一步区分使用的是哪一类存储。
代码如下,希望对你能有点参考:
private final BroadcastReceiver broadcastRec = new BroadcastReceiver(){
public void onReceive(Context context, Intent intent){
if(intent.getAction().equals("android.intent.action.MEDIA_MOUNTED"))
{
File file=new File("/sdcard/card/");
File file2=new File("/usb/");
if (file.exists() && !isSdcard) { //判断SD卡 插入
isSdcard=true;
Log.d("explorer", "SDcard is useful!");
Toast.makeText(context, R.string.SDCardin, Toast.LENGTH_LONG).show();
btn_sdcard.setEnabled(true);
String strPath="/sdcard/card/";
path_list.AddPathToList(strPath);
updateFileList();
}
else if(file2.exists() && !isUsb) { //判断usb 接入
//
isUsb=true;
Log.d("explorer", "USB is useful!");
Toast.makeText(context, R.string.USBin, Toast.LENGTH_LONG).show();
btn_usb.setEnabled(true);
String strPath="/usb/";
path_list.AddPathToList(strPath);//显示当前路径为usb
updateFileList();//更新文件显示列表
}
}else if(intent.getAction().equals("android.intent.action.MEDIA_REMOVED")
||intent.getAction().equals("android.intent.action.MEDIA_UNMOUNTED")
||intent.getAction().equals("android.intent.action.MEDIA_BAD_REMOVAL")){
File file=new File("/sdcard/card/");
File file2=new File("/usb/");
if (!file.exists() && isSdcard) {
Log.d("explorer", "SDcard is not useful!");
Toast.makeText(context, R.string.SDCardout, Toast.LENGTH_LONG).show();
isSdcard=false;
btn_sdcard.setEnabled(false);
if(path_list.GetCurPath().startsWith("/sdcard/")){
select_list.ClearList();
path_list.AddPathToList("/");
}
}
else if(!file2.exists() && isUsb){
isUsb=false;
Log.d("explorer", "USB is not useful!");
Toast.makeText(context, R.string.USBout, Toast.LENGTH_LONG).show();
btn_usb.setEnabled(false);
if(path_list.GetCurPath().startsWith("/usb/")){
select_list.ClearList();
path_list.AddPathToList("/");
}
}
updateFileList();
}
}
};
以上是广播接收外接存储插拔状态的。还有就是程序启动时判断usb和sdcard是否存在的:
private void initList(){
File file=new File("/sdcard/card/");
if (file.exists()) {
Log.d("explorer", "SDcard is useful!");
isSdcard=true;
btn_sdcard.setEnabled(true);
}else {
// 当前不可用
isSdcard=false;
Log.d("explorer", "SDcard is no use!");
btn_sdcard.setEnabled(false);
}
String strPath="/";
File file2=new File("/usb/");
if (file2.exists()) {
isUsb=true;
btn_usb.setEnabled(true);
if (isSdcard) {
strPath="/sdcard/card/";
}else {
strPath="/usb/";
}
}else {
isUsb=false;
btn_usb.setEnabled(false);
if (isSdcard) {
strPath="/sdcard/card/";
}else {
strPath="/";
}
}
path_list.AddPathToList(strPath);
updateFileList();
}
如果你是想同时识别多个存储设备,我提供个思路,没有试验过:
在android广播插入存储设备时,它的intent中有data,用方法getDataString()获取其中数据你会发现那里面是这个设备的路径,你根据自己的需要截取路径,并根据这个来识别不同的存储设备。
String path=intent.getDataString();
path=path.substring(11)+"/";//path=file:///mnt/sdcard/external_sdcard
以通过shell命令来查看 mount 相关信息,从而判断出已挂载的设备路径。
比如
root@android:/ # mount
rootfs / rootfs ro,relatime 0 0
tmpfs /dev tmpfs rw,nosuid,relatime,mode=755 0 0
devpts /dev/pts devpts rw,relatime,mode=600 0 0
proc /proc proc rw,relatime 0 0
sysfs /sys sysfs rw,relatime 0 0
none /acct cgroup rw,relatime,cpuacct 0 0
tmpfs /mnt/asec tmpfs rw,relatime,mode=755,gid=1000 0 0
tmpfs /mnt/obb tmpfs rw,relatime,mode=755,gid=1000 0 0
none /dev/cpuctl cgroup rw,relatime,cpu 0 0
/dev/block/mmcblk0p12 /system ext4 rw,relatime,user_xattr,barrier=1,data=ordered 0 0
/dev/block/mmcblk0p13 /data ext4 rw,nosuid,nodev,relatime,user_xattr,barrier=1,data=ordered,noauto_da_alloc 0 0
/dev/block/mmcblk0p15 /cache ext4 rw,nosuid,nodev,relatime,user_xattr,barrier=1,data=ordered 0 0
/dev/block/vold/179:18 /mnt/sdcard2 vfat rw,dirsync,nosuid,nodev,noexec,relatime,uid=1000,gid=1015,fmask=0000,dmask=0000,allow_utime=0022,codepage=cp437,iocharset=iso8859-1,shortname=mixed,utf8,errors=remount-ro 0 0