How to read the SD Card ID number?

后端 未结 12 2249
[愿得一人]
[愿得一人] 2020-12-05 08:15

How can I programatically read the SD Card\'s CID register, which contains a Serial Number and other information? Can I do it through Android Java, or Native code?

12条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-05 08:44

    Here is sample code for getting SID and CID

    if (isExteranlStorageAvailable()) {
        try {
            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++) {
                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 CID = new BufferedReader(new FileReader(
                    cid_directory + "/cid"));
            String sd_cid = CID.readLine();
            Log.d(TAG, "CID of the MMC = " + sd_cid);
    
        } catch (Exception e) {
            Log.e("CID_APP", "Can not read SD-card cid");
        }
    
    } else {
        Toast.makeText(this, "External Storage Not available!!",
                Toast.LENGTH_SHORT).show();
    }
    

提交回复
热议问题