如何获得SD卡ID /序列号?

时间:2017-11-30 06:24:16

标签: android android-sdcard serial-number

我想获得SDCard ID。

我在Nougat(7.0)及以上操作系统中遇到问题。

我已经尝试过了,

     private String getExternalSdCARDId() {
        try {
            String exsdcard_path = "/sys/block/mmcblk1";
            File file = new File(exsdcard_path);
            String memBlk = null;
            if (file.exists() && file.isDirectory()) {
                memBlk = "mmcblk1";
            } else {
                System.out.println("not a directory");
                memBlk = "mmcblk0";
            }
            Process cmd = Runtime.getRuntime().exec("cat /sys/block/" + memBlk + "/device/cid");
            BufferedReader br = new BufferedReader(new InputStreamReader(cmd.getInputStream()));
            return br.readLine();
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

但是这个方法返回NULL。

1 个答案:

答案 0 :(得分:1)

试试这个:

String path2 = "/sys/block/mmcblk0/device";

BufferedReader serial;
BufferedReader name ;
BufferedReader manfid;
BufferedReader oemid;
BufferedReader mfDate;
BufferedReader CID;

        serial = new BufferedReader(new FileReader(path2 + "/serial"));
        name = new BufferedReader(new FileReader(path2 + "/name"));
        manfid = new BufferedReader(new FileReader(path2 + "/manfid"));
        oemid = new BufferedReader(new FileReader(path2 + "/oemid"));
        mfDate = new BufferedReader(new FileReader(path2 + "/date"));
        CID = new BufferedReader(new FileReader(path2 + "/cid"));

    String sdSerial = serial.readLine();
    String sdName = name.readLine();
    String sdMfId = manfid.readLine();
    String sdOemId = oemid.readLine();
    String sdMfDate = mfDate.readLine();
    String sdCid = CID.readLine();

或者也可以使用此

File input = new File("/sys/class/mmc_host/mmc1");
    String cid_directory = null;
    int i = 0;
    File[] sid = input.listFiles();
    for (i = 0; i < sid.length; i++) {
        Log.d(TAG,"sid info "+sid[i]);
        if (sid[i].toString().contains("mmc1:")) {
            cid_directory = sid[i].toString();
            String SID = (String) sid[i].toString().subSequence(cid_directory.length() - 4, cid_directory.length());
            Log.d(TAG, " SID of MMC = " + SID);
            break;
        }
    }
    BufferedReader serial = new BufferedReader(new FileReader(cid_directory + "/serial"));
    BufferedReader name = new BufferedReader(new FileReader(cid_directory + "/name"));
    BufferedReader manfid = new BufferedReader(new FileReader(cid_directory + "/manfid"));
    BufferedReader oemid = new BufferedReader(new FileReader(cid_directory + "/oemid"));
    BufferedReader mfDate = new BufferedReader(new FileReader(cid_directory + "/date"));
    BufferedReader CID = new BufferedReader(new FileReader(cid_directory + "/cid"));


    String sdSerial = serial.readLine();
    String sdName = name.readLine();
    String sdMfId = manfid.readLine();
    String sdOemId = oemid.readLine();
    String sdMfDate = mfDate.readLine();
    String sdCid = CID.readLine();

//确保在使用此代码之前检查sdcard是否存在;

7.0以上:使用 您从StorageManager获得的StorageVolume上的StorageVolume.getUuid()

该值是在格式化卡期间分配的卷ID,其长度/格式因文件系统类型而异。对于FAT32,它是XXXX-XXXX,对于NTFS,它是更长的十六进制字符串,对于内部大容量存储,它返回null。