android:查找 SD 卡是否存在

发布于 2024-11-29 14:54:47 字数 205 浏览 2 评论 0原文

我遇到问题了。我有一个 Galaxy Tab,配有 16GB 内部存储空间和 2GB SD 卡。

当我使用environment.getExternalStorageState时,它返回mounted。但是,当我从 Galaxy Tab 中取出 SD 卡时,它仍然返回已安装状态,因为它将内部存储视为外部存储。有什么方法可以区分实际的 SD 卡和内部存储吗?

谢谢

I am having a problem. I have a galaxy tab that comes with 16gb of internal storage and 2 gb of sd-card card.

when I use environment.getExternalStorageState, it returns mounted. However, when I remove the sd-card from my galaxy tab, it still returns mounted because it considers the internal storage as the external storage. Is there any way in which I can differentiate between the actual SD-Card and the Internal Storage?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

反话 2024-12-06 14:54:47

目前似乎没有任何公共 API。您可以通过查看 /proc/mounts 进行检查,但您的代码将取决于设备,因为并非所有设备都在同一位置安装辅助外部存储(SD 卡)。

There doesn't seem to be any public API for this currently. You could check by looking at /proc/mounts but your code would then be device-dependent, since not all devices mount secondary external storage (SD card) at the same place.

瑾夏年华 2024-12-06 14:54:47

如果存在两个存储选项,我认为 /storage/sdcard0/ 用于内部 SD 卡,/storage/sdcard1/ 用于外部 SD 卡。
如果您要检查某个文件是否存在于任何 SD 卡中,您应该检查所有可能的路径。

String path;    
if(new File("/mnt/sdcard/yourpath").exists()) {
    path="/storage/sdcard/yourpath";
} else if(new File("/storage/sdcard0/yourpath").exists()) {
    path="/storage/sdcard0/yourpath";
} else if(new File("/storage/sdcard1/yourpath").exists()) {
    path="/storage/sdcard1/yourpath";
}

I think /storage/sdcard0/ is used for internal sdcard and /storage/sdcard1/ is used for external sd card if there are two storage option present.
if you are checking a file is present either in any of sdcard or not you should check all possible paths.

String path;    
if(new File("/mnt/sdcard/yourpath").exists()) {
    path="/storage/sdcard/yourpath";
} else if(new File("/storage/sdcard0/yourpath").exists()) {
    path="/storage/sdcard0/yourpath";
} else if(new File("/storage/sdcard1/yourpath").exists()) {
    path="/storage/sdcard1/yourpath";
}
红颜悴 2024-12-06 14:54:47

我自己创建了一张支票,并且有效。它检查辅助的、真实的 SD 卡是否存在。
在三星 Galaxy s5 neo、阿尔卡特 one touch 5020x 和 HTC One X 上进行了测试。
该代码应该适用于 KITKAT 设备,因为它使用应用程序默认目录进行检查。

我在主存储上创建了一个默认应用程序路径存储的字符串。
然后将“主要”更改为“辅助”,然后尝试创建一个文件夹并检查是否存在。

代码如下:

String primaryStorage = Environment.getExternalStorageDirectory().getAbsolutePath();
String secondaryStorage = System.getenv("SECONDARY_STORAGE");
Boolean hasSecondary = false;

String internalSD = getExternalFilesDir(null) + "/test";
String externalSD = internalSD.replace(primaryStorage, secondaryStorage);

            try{
                File dir = new File(externalSD);
                dir.mkdirs();

                if (dir.isDirectory()) {
                    dir.delete();
                    hasSecondary = true;
                }                   
            } catch (Exception e) {
            }

I've created a check of my own, and it works. It checks if secondary, real SD card is present.
Tested it on Samsung Galaxy s5 neo, Alcatel one touch 5020x, and on HTC One X.
The code should work on KITKAT devices since it uses the app default dir to check.

I create a String of default app path storage on primary storage.
Then change "primary" to "secondary", then try to create a folder and check for existance.

Heres the code:

String primaryStorage = Environment.getExternalStorageDirectory().getAbsolutePath();
String secondaryStorage = System.getenv("SECONDARY_STORAGE");
Boolean hasSecondary = false;

String internalSD = getExternalFilesDir(null) + "/test";
String externalSD = internalSD.replace(primaryStorage, secondaryStorage);

            try{
                File dir = new File(externalSD);
                dir.mkdirs();

                if (dir.isDirectory()) {
                    dir.delete();
                    hasSecondary = true;
                }                   
            } catch (Exception e) {
            }
雪若未夕 2024-12-06 14:54:47

我尝试这样做

    if(android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) 
{

    Log.i("tag", "SDCard is mounted");

    }

这个例子会帮助你

I try like this

    if(android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) 
{

    Log.i("tag", "SDCard is mounted");

    }

or this example would help you

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文