Android以编程方式获取SD卡的ID

时间:2012-01-30 11:31:13

标签: android uniqueidentifier android-sdcard

有什么方法可以通过编程方式获取Android设备中当前SD卡的特定标识符/序列号?

2 个答案:

答案 0 :(得分:2)

是的,试试这个:

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;
    }

答案 1 :(得分:1)

试试这个

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

这将为您提供整个存储卡详细信息