Android get id of SD Card programmatically

前端 未结 2 1130
情深已故
情深已故 2021-01-25 08:17

Is there any way which I can get some specific identifier/serial number of current SD Card in Android devices programmatically?

相关标签:
2条回答
  • 2021-01-25 09:03

    try this

     **way 1**     
    
    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();
    
    **way 2**
    
    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();
    

    this will give you the entire memory card details

    0 讨论(0)
  • 2021-01-25 09:09

    Yes, try this:

    public String getSDCARDiD()
        {
            String sd_cid = null;
            try {
    
                File file = new File("/sys/block/mmcblk1");
                String memBlk;
                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()));
                sd_cid = br.readLine();
                //System.out.println(sd_cid);
    
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return sd_cid;
        }
    
    0 讨论(0)
提交回复
热议问题