我想获取SDCard ID。

我在牛轧糖(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。

最佳答案

尝试这个 :

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以上:使用
StorageVolume.getUuid()关于从StorageManager获得的StorageVolume。

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

关于android - 如何获取SD卡ID/序列号?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47567116/

10-11 05:48