使用Runtime类 识别 sdcard和U盘android 6.0 存储路径识别

本文介绍了一种无需系统权限即可识别Android设备上外部存储的方法,包括SD卡和USB设备。通过解析proc/partitions和proc/mounts文件来获取主设备和次设备号,从而区分不同类型的外部存储。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

不需要系统权限!!!

理论:proc/partitions可以获取主设备和次设备号(sd[a/b/c]为u盘,mmcblk[数字][p为分区]),

proc/mounts可以获取/dev/block/vold/public:179,129 /mnt/media_rw/0403-0201 也包含了主设备和次设备号

(注意凡是外部存储(sdcard 和u盘)都是/dev/block/vold/...形式,android 6.0后形式有所变化为/dev/block/vold/public:179,129)

linux执行方法:

public static String execRootCmd(String cmd) {
		String result = "";
		DataOutputStream dos = null;
		DataInputStream dis = null;
		try {
			Process p = Runtime.getRuntime().exec(cmd);
			dos = new DataOutputStream(p.getOutputStream());
			dis = new DataInputStream(p.getInputStream());


	
			dos.writeBytes("exit\n");
			dos.flush();

			String line = null;
			while ((line = dis.readLine()) != null) {
				result += line;
			}
			p.waitFor();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (dos != null) {
				try {
					dos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (dis != null) {
				try {
					dis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return result;
	}

识别方法:

  private final static String ROOT_PATH="/storage/";
    public static void distinguishExternalPath(List<String> externalSdcardPathParam, List<String> externalUsbPathParam) {
        List<String> externalSdcardPath = new ArrayList<String>();
        List<String> externalUsbPath = new ArrayList<String>();
        String s = LinuxCmdUtil.execRootCmd("cat proc/partitions");
        s = s.replaceAll("\\s+", "|");
     
        String[] partitions = s.split("\\|");

        final List<String> usbStoreMajorMinor = new ArrayList<>();
        int repeatCurrent = 0;
        for (String item : partitions) {//get usb storage major and minor!!!!
            if (item.contains("sd")) {
                String majorMinor = partitions[repeatCurrent - 3] + "|" + partitions[repeatCurrent - 2];
               
                usbStoreMajorMinor.add(majorMinor);
            }
            repeatCurrent++;
        }

        String condition = "/dev/block/vold";
        String s1 = LinuxCmdUtil.execRootCmd("cat proc/mounts");
      
        String[] mounts = s1.split(" ");
        int currentRepeatCount = 0;
        for (String item : mounts) {
            if (item.length() > condition.length() && item.contains(condition)) {
                boolean contains = false;
                for (String usbMajorMinor : usbStoreMajorMinor) {
                    String[] split = usbMajorMinor.split("\\|");
                    contains = item.contains(split[0]) && item.contains(split[1]);
                    if (contains)break;
                }

                if (contains) {
                    String mount = mounts[currentRepeatCount + 1];
                    if (!externalUsbPath.contains(mount)) {
                     
                        externalUsbPath.add(mounts[currentRepeatCount + 1]);
                    }
                } else {
                    String mount = mounts[currentRepeatCount + 1];
                    if (!externalSdcardPath.contains(mount)) {
                      
                        externalSdcardPath.add(mount);
                    }
                }
            }
            currentRepeatCount++;
        }

        for(String usbItemPath:externalUsbPath){
            String[] split = usbItemPath.split("/");
            String dirName=split[split.length-1];
            String storagePath=ROOT_PATH+dirName;
            File file=new File(storagePath);
            boolean b = file.exists() && file.isDirectory();
        
            if (b){
                externalUsbPathParam.add(storagePath);
            }
        }

        for(String sdcardPath:externalSdcardPath){
            String[] split = sdcardPath.split("/");
            String dirName=split[split.length-1];
            String storagePath=ROOT_PATH+dirName;

            File file=new File(storagePath);
            boolean b = file.exists() && file.isDirectory();
         
            if (b){
                externalSdcardPathParam.add(storagePath);
            }
        }
    }
由于测试时发现mnt/media_rw/....用File.exist 和File.isDirectory发现不认为是文件夹(都是false),所以不能直接使用这种路径,所以后面的两个for循环就是为了解决这个问题的(/storage目录下是可以识别为文件夹的)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值