Android get id of SD Card programmatically
Asked Answered
C

2

2

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

Chrisoula answered 30/1, 2012 at 11:31 Comment(1)
Try this: #3349143Lindsley
P
3

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

Politburo answered 3/11, 2017 at 10:28 Comment(0)
A
2

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;
    }
Absa answered 21/5, 2012 at 16:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.