Android get id of SD Card programmatically

大兔子大兔子 提交于 2019-12-31 04:27:07

问题


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


回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/9063161/android-get-id-of-sd-card-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!