android系统如何获得外置卡路径

来源:互联网 发布:淘宝上门安装怎么设置 编辑:程序博客网 时间:2024/04/30 02:56

1、Android中查看USB连接的外接设备信息的代码实例

1,USB存储设备(如:U盘,移动硬盘):

 //USB存储设备 插拔监听与 SD卡插拔监听一致。

private USBBroadCastReceiver mBroadcastReceiver;     IntentFilter iFilter = new IntentFilter();       iFilter.addAction(Intent.ACTION_MEDIA_EJECT);       iFilter.addAction(Intent.ACTION_MEDIA_MOUNTED);       iFilter.addAction(Intent.ACTION_MEDIA_REMOVED);       iFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);       iFilter.addDataScheme("file");       mBroadcastReceiver = new USBBroadCastReceiver();       registerReceiver(mBroadcastReceiver, iFilter);private class USBBroadCastReceiver extends BroadcastReceiver {      @Override      public void onReceive(Context context, Intent intent) {       String action = intent.getAction();       if (action.equals(Intent.ACTION_MEDIA_EJECT)) {          //USB设备移除,更新UI            } else if (action.equals(Intent.ACTION_MEDIA_MOUNTED)) {          //USB设备挂载,更新UI        }      } }  //获得挂载的USB设备的存储空间使用情况public static String getUSBStorage(Context context){      // USB Storage       //storage/udisk为USB设备在Android设备上的挂载路径.不同厂商的Android设备路径不同。      //这样写同样适合于SD卡挂载。      File path = new File("/storage/udisk");      StatFs stat = new StatFs(path.getPath());      long blockSize = stat.getBlockSize();      long totalBlocks = stat.getBlockCount();      long availableBlocks = stat.getAvailableBlocks();      String usedSize = Formatter.formatFileSize(context, (totalBlocks-availableBlocks) * blockSize);      String availableSize = Formatter.formatFileSize(context, availableBlocks * blockSize);      return usedSize + " / " + availableSize;//空间:已使用/可用的 }



2,USB外接输入设备(如:键盘,鼠标,扫描枪)

try {     //获得外接USB输入设备的信息     Process p=Runtime.getRuntime().exec("cat /proc/bus/input/devices");     BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));     String line = null;     while((line = in.readLine())!= null){       String deviceInfo = line.trim();       //对获取的每行的设备信息进行过滤,获得自己想要的。     }       } catch (Exception e) {  // TODO: handle exception     e.printStackTrace();    }

另:设备信息也可以通过 adb shell 进入执行  cat /proc/bus/input/devices看到。

USB外接输入设备信息打印如下:

I: Bus=0003 Vendor=11c0 Product=0030 Version=0110N: Name="ACRUX USB Keyboard"P: Phys=usb-0000:00:04.0-1.3/input1S: Sysfs=/devices/pci0000:00/0000:00:04.0/usb1/1-1/1-1.3/1-1.3:1.1/input/input3U: Uniq=H: Handlers=mouse1 event3 B: PROP=0B: EV=17B: KEY=70000 0 0 0 0 0 0 0 0B: REL=103B: MSC=10

I: Bus=0003 Vendor=11c0 Product=0030 Version=0110 这行信息会在Android设备与USB设备数据交互的是否使用到。

关于USB外接设备如何与Android设备数据数据交互的代码书写可以参考:

http://developer.android.com/guide/topics/connectivity/usb/index.html

N: Name="ACRUX USB Keyboard" 这行说明了外接的USB设备的名称。

P: Phys=usb-0000:00:04.0-1.3/input1

S: Sysfs=/devices/pci0000:00/0000:00:04.0/usb1/1-1/1-1.3/1-1.3:1.1/input/input3

这两行可用于识别该USB设备连接在Android设备的哪一个USB口.


2、内置与外置卡路径实际是存在系统文件"system/etc/vold.fstab"中,可以用一下语句获得路径。

Environment.getRootDirectory().getAbsoluteFile() + File.separator + "etc" + File.separator + "vold.fstab";

联想K860i的system/etc/vold.fstab文件内容如下:

## Vold 2.0 Generic fstab
## - San Mehat (san@android.com)
##


#######################
## Regular device mount
##
## Format: dev_mount <label> <mount_point> <part> <sysfs_path1...>
## label        - Label for the volume
## mount_point  - Where the volume will be mounted
## part         - Partition # (1 based), or 'auto' for first usable partition.
## <sysfs_path> - List of sysfs paths to source devices
######################
dev_mount sdcard /mnt/sdcard 1 /devices/platform/dw_mmc/mmc_host/mmc0
dev_mount extra_sd 
/mnt/sdcard/extra_sd auto /devices/platform/s3c-sdhci.2/mmc_host/mmc1

联想K860i的sd卡路径是:

mnt/sdcard 内置卡,mnt/sdcard/extra_sd外置卡,与结果相符。

目前只在联想K860i和公司的样机G2,G10,G6+,D6上测试OK。

G2,/storage/sdcard0内置卡,/storage/sdcard1外置卡

dev_mount sdcard /storage/sdcard0 emmc@fat /devices/platform/goldfish_mmc.0 /devices/platform/mtk-sd.0/mmc_host
dev_mount sdcard2 /storage/sdcard1 auto /devices/platform/goldfish_mmc.1 /devices/platform/mtk-sd.1/mmc_host

G10&G10+,/mnt/sdcard外置卡,/mnt/external_sd外置卡,/mnt/usb_storageU盘

dev_mount flash /mnt/sdcard auto /dev/block/mtd/by-name/user 
dev_mount sdcard /mnt/external_sd auto /devices/platform/rk29_sdmmc.0/mmc_host/mmc0
dev_mount udisk /mnt/usb_storage auto /devices/platform/usb20_host/usb /devices/platform/usb20_otg/usb

G6+,/mnt/sdcard内置卡,/mnt/external_sd外置卡

dev_mount flash /mnt/sdcard auto /devices/virtual/mtd/mtd9/mtdblock9
dev_mount sdcard /mnt/external_sd auto /devices/platform/rk29_sdmmc.0/mmc_host/mmc0

D6,/mnt/sdcard内置卡,/mnt/extsd外置卡,/mnt/usbhost1U盘

dev_mount sdcard/mnt/sdcardauto/devices/virtual/block/nandj
dev_mount extsd/mnt/extsdauto/devices/platform/sunxi-mmc.1/mmc_host/devices/platform/sunxi-mmc.0/mmc_host
dev_mount usbhost1/mnt/usbhost1auto/devices/platform/sw-ehci.1/devices/platform/sw_hcd_host0/devices/platform/sw-ohci.1

G6,,/mnt/sdcard内置卡,/mnt/extsd外置卡,/mnt/usbhost1U盘

dev_mount sdcard/mnt/sdcardauto/devices/virtual/block/nandj
dev_mount extsd/mnt/extsdauto/devices/platform/sunxi-mmc.1/mmc_host/devices/platform/sunxi-mmc.0/mmc_host
dev_mount usbhost1/mnt/usbhost1auto/devices/platform/sw-ehci.1/devices/platform/sw_hcd_host0/devices/platform/sw-ehci.2a

D6和G6有所不同,不是用空格隔开,而是用TAB隔开。

获取盘符类:public class Dev_MountInfo implements IDev {  /*    #######################    ## Regular device mount    ##    ## Format: dev_mount <label> <mount_point> <part> <sysfs_path1...>     ## label        - Label for the volume    ## mount_point  - Where the volume will be mounted    ## part         - Partition # (1 based), or 'auto' for first usable partition.    ## <sysfs_path> - List of sysfs paths to source devices    ######################    */    public final String HEAD = "dev_mount";      private final int DEV_INTERNAL = 0;    private final int DEV_EXTERNAL = 1;    private final int DEV_USB = 2;//这个可能没有,上面两个一般都会有      private ArrayList<String> cache = new ArrayList<String>();        private static Dev_MountInfo dev;      private DevInfo info;      private final File VOLD_FSTAB = new File(    Environment.getRootDirectory().getAbsoluteFile()            + File.separator + "etc"            + File.separator + "vold.fstab");    public static Dev_MountInfo getInstance() {          if (null == dev)              dev = new Dev_MountInfo();          return dev;      }      private DevInfo getInfo(final int device) {          try {              initVoldFstabToCache();          } catch (IOException e) {              e.printStackTrace();          }            info = new DevInfo();        if(device < cache.size()){        String[] sinfo = cache.get(device).split(" |\t");        if(sinfo.length >= 5){        info.setLabel(sinfo[1]);        info.setMount_point(sinfo[2]);        info.setPart(sinfo[3]);        info.setSysfs_path(sinfo[4]);        }        }        return info;      }      /**      * init the words into the cache array      * @throws IOException      */      private void initVoldFstabToCache() throws IOException {          cache.clear();          BufferedReader br = new BufferedReader(new FileReader(VOLD_FSTAB));        String tmp = null;          while ((tmp = br.readLine()) != null) {              // the words startsWith "dev_mount" are the SD info              if (tmp.startsWith(HEAD)) {                  cache.add(tmp);                  Log.v("xcl", tmp);            }          }          br.close();          cache.trimToSize();      }      public class DevInfo {          private String label, mount_point, part;        private String sysfs_path;          public String getLabel() {              return label;          }            private void setLabel(String label) {              this.label = label;          }          public String getMount_point() {              return mount_point;          }            private void setMount_point(String mount_point) {              this.mount_point = mount_point;          }          public String getPart() {              return part;          }            private void setPart(String part) {              this.part = part;          }          public String getSysfs_path() {              return sysfs_path;          }            private void setSysfs_path(String sysfs_path) {              this.sysfs_path = sysfs_path;          }        }        public DevInfo getInternalInfo() {        return getInfo(DEV_INTERNAL);    }      public DevInfo getExternalInfo() {        return getInfo(DEV_EXTERNAL);    }        public DevInfo getUSBInfo(){    return getInfo(DEV_USB);    }}    interface IDev {      DevInfo getInternalInfo();        DevInfo getExternalInfo();  }  调用代码://获得各盘符路径    Dev_MountInfo dev = Dev_MountInfo.getInstance();      DevInfo info = dev.getInternalInfo();    this.diskInternal = info.getMount_point();    //Environment.getExternalStorageDirectory() 这两个要是不一样咋办    info = dev.getExternalInfo();     this.diskExternal = info.getMount_point();    info = dev.getUSBInfo();     this.diskUSB = info.getMount_point();

3、android系统根目录下有/proc/mounts文件,里面有所有挂载信息,用以上方法应该可以判断某个路径是否挂载

private static final String MOUNTS_FILE = "/proc/mounts";public static boolean isMounted(String path) {        boolean blnRet = false;        String strLine = null;        BufferedReader reader = null;        try {            reader = new BufferedReader(new FileReader(MOUNTS_FILE));            while ((strLine = reader.readLine()) != null) {                if (strLine.contains(path)) {                    blnRet = true;                    break;                }            }        } catch (Exception e) {            e.printStackTrace();        } finally {            if (reader != null) {                try {                    reader.close();                } catch (IOException e) {                    e.printStackTrace();                }                reader = null;            }        }        return blnRet;    }









0 0
原创粉丝点击