Method for Android to obtain internal and external memory cards

In previous Android (versions before 4.1), the sdcard and path represented the memory card through "/ sdcard" or "/ MNT / sdcard". In the Jerry bean system, it was modified to "/ storage / sdcard0", and there may be multiple sdcards in the future.

At present, in order to maintain compatibility with the previous code, the sdcard path is link mapped.

In order to make your code more robust and compatible with future Android versions and new devices, please obtain the sdcard path through environment. Getexternalstoragedirectory(). Getpath(). If you need to save specific types of content to the sdcard, you can consider using the environment.getexternalstoragepublicdirectory (string type) function, This function can return a specific type of directory. Currently, the following types are supported:

If your application needs to download the above types of files, you can put them in the corresponding directory above to help users find them. For example, the most commonly used is to download files. If you develop a browser and download the file to the download directory when downloading the file, it is convenient for users to find the file in the future. Of course, if you want users to view the file they downloaded by starting your program, you can also not do so ^ ^ ^.

When using these directories to save files, one thing to note: other programs may also use these directories. Before saving files, pay attention to judge whether the file already exists and do not overwrite the user's previous data.

After Android 4.1, Android has added support for multiple memory cards. Generally, mobile phones have built-in memory cards and external memory cards, or multiple external memory cards. How to get the memory card path?

In particular, the memory paths of various Android devices are different. For example, the T card path may be / MNT / sdcard, / MNT / extsd, / MNT / external_ SD or / MNT / sdcard2. Sometimes the path of built-in memory may also be / MNT / sdcard, and the path of host USB memory is also various.

The following method is to obtain the storage list by calling the hidden interface getvolumepaths() of storagemanager through reflection.

In Android 2.3, judge whether the built-in SD card is attached:

Determine whether the external SD card is attached:

Incidentally, describe how to obtain the space size of the sdcard:

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>