简体   繁体   English

Android上的SD卡位置

[英]SD Card Location on Android

I'm using Bluestacks for testing my app, because I don't have Androids lying around. 我正在使用Bluestack来测试我的应用程序,因为我没有躺着的Androids。 I'm tring to write a file to the SDCard but can't seem to figure out the path for it. 我想把一个文件写入SD卡,但似乎无法弄清楚它的路径。 I've tried the following: /mnt/sdcard/ext_sd & /mnt/extSdCard but neither of those worked. 我尝试了以下内容: /mnt/sdcard/ext_sd/mnt/extSdCard但这些都没有。

I've tried the following: /mnt/sdcard/ext_sd & /mnt/extSdCard but neither of those worked. 我尝试了以下内容: /mnt/sdcard/ext_sd/mnt/extSdCard但这些都没有。

You should not hardcode paths. 你不应该硬编码路径。 Because SD card storage location or path varies from phone to phone. 因为SD卡存储位置或路径因手机而异。 SD card storage location in my phone is /storage/sdcard1 我手机中的SD卡存储位置是/storage/sdcard1

Now coming to your question, 现在回答你的问题,

Before API level 19 , there was no official API method to store in SD card. API级别19之前, 没有正式的API method存储在SD卡中。 But, many could do it using unofficial libraries or APIs. 但是,许多人可以使用非官方库或API来实现。

Officially, one method ( getExternalFilesDirs ) was introduced in Context class in API level 19 (Android version 4.4 - Kitkat). 正式地,在API级别19(Android版本4.4 - Kitkat)的Context类中引入了一个方法( getExternalFilesDirs )。

File[] getExternalFilesDirs (String type)

It returns absolute paths to application-specific directories on all shared/external storage devices where the application can place persistent files it owns. 它返回应用程序可以放置其拥有的持久文件的所有共享/外部存储设备上特定于应用程序的目录的绝对路径。 These files are internal to the application, and not typically visible to the user as media. 这些文件是应用程序的内部文件,通常不会被用户视为媒体。

That means, it will return paths to both types Storage - Internal memory and Micro SD card. 这意味着,它将返回两种类型存储 - 内存和Micro SD卡的路径。 Generally, second returned path would be storage path of micro SD card(but not always). 通常, 第二返回路径将是微SD卡的存储路径(但并非总是如此)。 So you need to check it out by executing the code with this method. 因此,您需要通过使用此方法执行代码来检查它。

Instead of hardcoding paths, you should use this method in your app source code to get the SD card location. 您应该在应用源代码中使用此方法来获取SD卡位置,而不是硬编码路径。 Then, write files to that location. 然后,将文件写入该位置。

If you want to know more about storage location or paths in Android, please go through my other answer 如果您想了解有关Android中存储位置或路径的更多信息,请查看我的其他答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM