How to read the SD Card ID number?

后端 未结 12 2114
[愿得一人]
[愿得一人] 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:45

    As BMB wrote, you cannot know the real path of the SD card in the /sys filesystem. However, /sys implements aliases as well, and you may be able to retrieve the CID through /sys/block/mmcblk0/device/cid.

    For example, on a Tattoo running 2.2, /sys/block/mmcblk0 is a soft link (established automatically by the system) to /sys/devices/platform/msm_sdcc.2/mmc_host/mmc1/mmc1:aaaa/block/mmcblk0.

    0 讨论(0)
  • 2020-12-05 08:47

    You can get it with the StorageManager.
    Here is a sample-code:

    final StorageManager storageManager = (StorageManager) activity.getSystemService(Context.STORAGE_SERVICE);
    final List<StorageVolume> storageVolumes = storageManager.getStorageVolumes();
    final UserHandle user = android.os.Process.myUserHandle();
    for (StorageVolume storageVolume : storageVolumes) {
         // "SDCARD" must be your sd-card name
         if (storageVolume.getDescription(activity).equals("SDCARD")){
            // the SD Card ID number
            return storageVolume.getUuid();
         }
     }
    
    0 讨论(0)
  • 2020-12-05 08:51

    The only code I've found thus far to provide the id is C++ or C# http://jo0ls-dotnet-stuff.blogspot.com/2008/12/read-secure-digital-sd-card-serial.html
    http://jo0ls-dotnet-stuff.blogspot.com/2008/12/read-cid-and-csd-c-implementation.html
    If you are a C++ developer you may be able to take this and make it work on Android.

    0 讨论(0)
  • 2020-12-05 08:56

    I managed to find my SD card CID by plugging my phone to my computer via usb and using the adb tool (Android SDK)

    $ adb shell
    # cat /sys/block/mmcblk0/../../cid
    

    My phone is rooted so I'm not sure if this is accessible on non-rooted phones.

    Also try

    $ adb shell
    # cd /sys/block/mmcblk0/../../
    # ls
    block                 fwrev                 preferred_erase_size
    cid                   hwrev                 scr
    csd                   manfid                serial
    date                  name                  subsystem
    driver                oemid                 type
    erase_size            power                 uevent
    

    These are explained in the kernel documentation http://www.mjmwired.net/kernel/Documentation/mmc/mmc-dev-attrs.txt

    0 讨论(0)
  • 2020-12-05 08:57

    I was looking for this function too but could never get a home brewed solution. I ended up finding a company that makes an SID reader. Go to nexcopy.com and look in their SD or microSD duplicator section. It can read up to 20 cards at a time.

    0 讨论(0)
  • 2020-12-05 08:58

    I made this one... it worked for me... hope it makes u clear!

    String getSDCARDiD()
        {
            try {
    
                File file = new File("/sys/block/mmcblk1");
                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)
提交回复
热议问题