Android 如何读取SD卡ID号?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3348643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:50:15  来源:igfitidea点击:

How to read the SD Card ID number?

androidc++windowsmfc

提问by Eric Hamilton

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?

如何以编程方式读取 SD 卡的 CID 寄存器,其中包含序列号和其他信息?我可以通过 Android Java 或本机代码来完成吗?

Thanks in advance, Eric

提前致谢,埃里克

回答by Dinesh Prajapati

Here is sample code for getting SID and CID

这是获取 SID 和 CID 的示例代码

if (isExteranlStorageAvailable()) {
    try {
        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++) {
            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 CID = new BufferedReader(new FileReader(
                cid_directory + "/cid"));
        String sd_cid = CID.readLine();
        Log.d(TAG, "CID of the MMC = " + sd_cid);

    } catch (Exception e) {
        Log.e("CID_APP", "Can not read SD-card cid");
    }

} else {
    Toast.makeText(this, "External Storage Not available!!",
            Toast.LENGTH_SHORT).show();
}

回答by delcypher

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

我设法通过 USB 将手机插入计算机并使用 adb 工具(Android SDK)找到了我的 SD 卡 CID

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

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

我的手机是 root 的,所以我不确定这是否可以在非 root 的手机上访问。

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

这些在内核文档http://www.mjmwired.net/kernel/Documentation/mmc/mmc-dev-attrs.txt 中有解释

回答by Aditya Nikhade

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

回答by Paul Gregtheitroade

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.

到目前为止,我发现提供 ID 的唯一代码是 C++ 或 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
如果您是 C++ 开发人员,您也许可以使用它并使其在 Android 上运行。

回答by southerton

There is api in android for getting sd card serial id. The method is called getFatVolumeId(String mountPoint), where "mountPoint" is sd card name (you can get this name by calling Environment.getExternalStorageDirectory()). But getFatVolumeId is kindof hidden function (or forgotten), so you want to create package in your project named android.os and the class in it to reference the native method, to be able to call it:

android中有用于获取sd卡序列号的api。该方法称为getFatVolumeId(String mountPoint),其中“mountPoint”为sd 卡名称(您可以通过调用Environment.getExternalStorageDirectory() 获取此名称)。但是 getFatVolumeId 是一种隐藏函数(或被遗忘的函数),因此您希望在名为 android.os 的项目中创建包以及其中的类来引用本机方法,以便能够调用它:

package android.os;

public class FileUtils {
    public static native int getFatVolumeId(String mountPoint);
}

And the code is:

代码是:

File SdCard = Environment.getExternalStorageDirectory();
int Serial = FileUtils.getFatVolumeId(SdCard.getName());

Also see the links

另请参阅链接

http://groups.google.com/group/android-framework/browse_thread/thread/1abd18435ba20a67?pli=1http://markmail.org/message/rdl4bhwywywhhiau#query:+page:1+mid:rdl4bhwywywhhiau+state:results

http://groups.google.com/group/android-framework/browse_thread/thread/1abd18435ba20a67?pli=1http://markmail.org/message/rdl4bhwywywhhiau#query:+page:1+mid:rdl4bhwywywhhiau+state:结果

回答by Samuel Tardieu

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.

正如 BMB 所写,您无法知道 /sys 文件系统中 SD 卡的真实路径。但是,/sys 也实现了别名,您可以通过 /sys/block/mmcblk0/device/cid 检索 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.

例如,在运行 2.2 的纹身上,/sys/block/mmcblk0 是一个软链接(由系统自动建立)到 /sys/devices/platform/msm_sdcc.2/mmc_host/mmc1/mmc1:aaaa/block/mmcblk0。

回答by BMB

There is no Android API that can do this that I am aware of and for a general solution that is needed. To provide some background the SD card is connected to a hw-controller that is specific to the device platform. It is possible to read out the cid value from the linux /sys file system if you know your platform. The following snippet works on my droid (TI omap based platform) but not on Qualcomm MSM based platforms.

我知道没有 Android API 可以做到这一点,也没有需要的通用解决方案。为了提供一些背景,SD 卡连接到特定于设备平台的硬件控制器。如果您了解您的平台,则可以从 linux /sys 文件系统中读出 cid 值。以下代码段适用于我的 droid(基于 TI omap 的平台),但不适用于基于 Qualcomm MSM 的平台。

try {
        BufferedReader input = new BufferedReader(new FileReader("/sys/devices/platform/mmci-omap-hs.0/mmc_host/mmc0/mmc0:aaaa/cid"));
        String sd_cid = input.readLine();
        Log.i("CID_APP", sd_cid);
    } catch (Exception e) {
        Log.e("CID_APP","Can not read SD-card cid");
    }

On a another platform the sys file we are looking for is different. It may even differ between different cards on the same platform since I was not able to test that. On MSM based devices the path would be something like /sys/devices/platform/msm_sdcc.1/mmc_host/...

在另一个平台上,我们正在寻找的 sys 文件是不同的。由于我无法测试,因此同一平台上的不同卡之间甚至可能有所不同。在基于 MSM 的设备上,路径类似于 /sys/devices/platform/msm_sdcc.1/mmc_host/...

Since we have this hardware dependence on reading out the SD-card CID there would have to be an update to the Android API:s that provides general access. This API would then have to be implemented by each device manufacturer to map to the correct sd card controller driver.

由于我们对读取 SD 卡 CID 有这种硬件依赖性,因此必须对提供通用访问的 Android API:s 进行更新。然后,每个设备制造商都必须实现此 API 以映射到正确的 SD 卡控制器驱动程序。

回答by Jesus

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.

我也在寻找此功能,但永远无法获得自制解决方案。我最终找到了一家制造 SID 阅读器的公司。转到 nexcopy.com 并查看他们的 SD 或 microSD 复制器部分。它一次最多可以读取 20 张卡片。

回答by yurenchen

回答by gregS

Even though the relatively ancient Palm OS and Windows Mobile OS devices are able to read the SD card ID, AFAIK Android devices aren't capable of doing that yet. This is particularly troubling given the problems with the Settings.Secure.ANDROID_ID discussed here.

尽管相对古老的 Palm OS 和 Windows Mobile OS 设备能够读取 SD 卡 ID,但 AFAIK Android 设备还不能这样做。鉴于此处讨论的 Settings.Secure.ANDROID_ID 存在问题,这尤其令人不安。