Summary of several methods for Android to obtain internal and external SD card paths

Android SD card path:

The path of external SD card may not be used by many students in their daily work, because many models do not support external SD card (this is also Google's goal), so the path of external SD card does not need to be considered. In addition to developing file management applications, other applications can meet their needs by using some static methods in the environment class. But there are also some special needs that need to use the external SD card path. How can we accurately obtain the path of the external SD card?

Method 1

Note: for more details, go to the enviroment source code.

Also note that in API version 23, secondary_ Storage was removed.

Method 2

Use the method getvolumelist() in the class stroagevolume hidden in the SDK to obtain all storage space (stroage volume) through reflection, and then use the parameter is_ Removable control to obtain the path of internal storage and external storage (internal and external SD cards). The parameter is_ When removable is false, the path of the built-in SD card is obtained; if it is true, the path of the external SD card is obtained.

In the internal class userenvironment of API 23 environment class, there is a method getexternaldirs, which is the same as this. The code is as follows:

Let's look at the implementation of getexternalstoragedirectory method of environment:

It can be seen that in API 23, first get the file [] array of all storage spaces through getexternaldirs(). The first value of this array: getexternaldirs() [0], which is the path where the built-in SD card is located.

In the version before API 23, there is no method similar to getexternaldirs() to directly obtain storage volume through storagevolume, which is implemented in other ways. See the source code of the key methods:

The function of getexternaldirsforapp () here is the same as that of getexternaldirs () above, which is to get the file [] array of all storage space.

Mexternaldirsforapp is initialized in the constructor of the internal class userenvironment in the environment class. The source code of the environment #userenvironment constructor is as follows:

You can also get a method to get the path of all storage space according to this method, getstoragedirectories():

To sum up, both method 1 and method 2 can correctly obtain the internal and external SD card paths, but the method has the following problems:

1. Method 1 is invalid when API > = 23 (not tested yet)

2. Some manufacturers have too many ROM changes and have problems with the support of relevant native APIs. At this time, there may be problems with method 1.

3. Other causes (basically similar to 2, caused by ROM and other factors)

Therefore, it is recommended to use method 2 to obtain the internal and external SD card path. It should also be OK to use getstoragedirectories() before API 23 (Android 6.0).

Thank you for reading, hope to help you, thank you for your support to this site!

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
分享
二维码
< <上一篇
下一篇>>