Android:外部 SD 卡的绝对位置

发布于 2024-12-01 23:45:40 字数 274 浏览 0 评论 0原文

我有一个非常简单的问题,但到目前为止我无法找到这个问题的答案。 “有没有办法在Android中找到INTERNAL STORAGE DIRECTORYEXTERNAL STORAGE DIRECTORY(SDCARD)的绝对路径?”

请不要建议使用 Environment.getExternalStorageDirectory 因为它通常返回内部存储的路径或 Android 操作系统默认选择的存储介质。

有什么建议吗?

I have a very simple question but so far I am unable to find an answer of this question. "Is there any way of finding the absolute path of INTERNAL STORAGE DIRECTORY and EXTERNAL STORAGE DIRECTORY(SDCARD) in Android?"

Please don't recommend using Environment.getExternalStorageDirectory since it usually returns the path for internal storage or WHATEVER storage media is selected as default by the Android operating system..

Any suggestions please?

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

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

发布评论

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

评论(6

不必在意 2024-12-08 23:45:40

之前有人问过这个问题,请使用搜索。简而言之,“外部存储”更像是“共享存储”,它可能会也可能不会由实际的 SD 卡实现。有些设备有一个额外的 SD 卡,不用作外部存储。如果这就是您所要求的,则当前没有用于访问其安装位置的公共 API,并且它因设备而异。您可以检查 /proc/mount 来查看当前安装的内容并从那里开始。

That's been asked before on SO, use the search. In short, 'external storage' is more like 'shared storage' and it may or may not be implemented by an actual SD card. Some devices have an additional SD card, not used as external storage. If that is what you are asking for, there is currently no public API for accessing its mount location, and it varies between devices. You can check /proc/mount to see what is currently mounted and go from there.

看轻我的陪伴 2024-12-08 23:45:40

此链接来自 Android 配置指南。我认为这是推荐的而不是必需的。

https://source.android.com/devices/tech/storage/config -example.html

因此,要获取 SDCARD,您就可以这样做。

String storagePath = System.getenv("SECONDARY_STORAGE");

if (storagePath == null) {
    return null;
} else {
     String[] storagePathArray = storagePath.split(":");
     return storagePathArray[0];
}

EXTERNAL_STORAGE 似乎是我自己的一切。

SECONDARY_STORAGE 是在我的 LG GTab 8.3 和我的 Samsung Tab 2&3 上定义的,但在我的 Galaxy S (2.3) 或我的 Dell Venue (4.3) 上没有定义。在三星设备上,它似乎首先包含 SD 卡的多个路径,因此进行了拆分。

This link is from the Android configuration guide. I assume it's recommend and is not required.

https://source.android.com/devices/tech/storage/config-example.html

So, to get the SDCARD, you can just do.

String storagePath = System.getenv("SECONDARY_STORAGE");

if (storagePath == null) {
    return null;
} else {
     String[] storagePathArray = storagePath.split(":");
     return storagePathArray[0];
}

EXTERNAL_STORAGE seems to be on everything on I own.

SECONDARY_STORAGE was defined on my LG GTab 8.3 and my Samsung Tab 2&3, but not on my Galaxy S (2.3) or my Dell Venue (4.3). On Samsung devices it seems to contained multiple paths with the SD card first, thus the split.

吹泡泡o 2024-12-08 23:45:40

你应该使用

Environment.getExternalStorageDirectory().getAbsolutePath()

You should use

Environment.getExternalStorageDirectory().getAbsolutePath()
得不到的就毁灭 2024-12-08 23:45:40

在三星 Galaxy Note 10.1 2014 版 SM-P600(我假设大多数其他三星 Galaxy Notes 同型号)上,“真实”外部 SD 卡的完整路径是:

/storage/extSdCard/

我发现我的使用终端并执行:

cd /storage/extSdCard/

然后进入卡的根目录后,我使用“ls”命令列出文件,这样我就可以验证存储的内容在它上面。我就是这样找到我的。希望它对其他人有帮助。

On Samsung Galaxy Note 10.1 2014 edition SM-P600 (and I would assume most other samsung galaxy notes of the same vintage), the full path to the "REAL" external sdcard, is this:

/storage/extSdCard/

I found mine using a terminal and did:

cd /storage/extSdCard/

then once in the root of the card, I used the "ls" command to list files, so I could verify what was stored on it. Thats how I found mine. Hope it helps someone else.

若有似无的小暗淡 2024-12-08 23:45:40

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

 String path;    
 if(new File("/storage/sdcard/yourpath").exist())
     {
        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("/storage/sdcard/yourpath").exist())
     {
        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-08 23:45:40

String path=ExternalStorageDirectoryPath + "/文件夹名称/";

String path=ExternalStorageDirectoryPath + "/foldername/";

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