Transferred from: http://my.oschina.net/liucundong/blog/288183
I have a ZTE Android phone, the model is ZTE U930HD, the phone is not plugged into the external SD card (that is, micro SD cards, formerly known as Trans-flash Card (TF), 2004 formally renamed Micro SD card), But the fuselage comes with a built-in memory card (i.e., eMMC storage, size 2G).
I plug this phone into a computer with a data cable, and I see a drive letter that installs「R.E Manager」such as file management applications, you can also manage files, and you can see that the storage Mount directory is:/mnt/sdcard2
But
I printed environment.getexternalstoragestate (), but returned "Removed“;
What's going on? Obviously the phone itself with the built-in SD card, but why prompted such a message?
I'm trying to print again.Environment.getexternalstoragedirectory (), return: "/mnt/sdcard"
Seems to explain that on my ZTE phone, callEnvironment.getexternalstoragedirectory (), the returned storage directory is not a system built-in SD card directory.
I changed another Sony l39u, a MOTO G,CallThe directory returned by Environment.getexternalstoragedirectory () is the SD card directory built into the system.
On different devices, callThe getExternalStorageDirectory () return value is not the same. Query the Android document, only to find the reason, originally this method returned is the current device manufacturers think of "external storage", it is possible to return to the external SD card directory (Micro SD Card), you may also return the built-inStorage (EMMC).
To summarize:
Some phones attach eMMC storage to nodes such as/MNT/EXTERNAL_SD,/mnt/sdcard2, and attach an external SD card to the Environment.getexternalstoragedirectory () node.
At this point, the Environment.getexternalstoragedirectory () is called, and the path to the external SD is returned.
While the other part of the phone directly emmc storage mounted on the environment.getexternalstoragedirectory () this node, and the real external SD card mounted to/MNT/EXTERNAL_SD,/mnt/sdcard2 and other nodes.
At this point, the Environment.getexternalstoragedirectory () is called, and the path to the built-in SD is returned.
To this point can be interpreted as both without external SD card case, on ZTE phone, call
Print environment.getexternalstoragestate (), but return "Removed", on Sony, MOTO G, return:"Mounted"
The reason is already known, but how to get to the specific path of this built-in EMMC storage when there is no external SD card?
For example, my ZTE phone, since the use of environment.getexternalstoragedirectory () to get the external SD card path, but I did not insert the SD card, This time I want to use the built-in EMMC storage to store some of the data used in the program, how can I get the path to this EMMC storage?
The answer is: by scanning the system file "System/etc/vold.fstab" to achieve.
"System/etc/vold.fstab" is just a simple configuration file that describes the mount point information for Android.
We can traverse this file to get all the mount points:
?
1234567891011121314151617 |
/**
* 遍历 "system/etc/vold.fstab” 文件,获取全部的Android的挂载点信息
*
* @return
*/
private
static ArrayList<String> getDevMountList() {
String[] toSearch = FileUtils.readFile(
"/etc/vold.fstab"
).split(
" "
);
ArrayList<String> out =
new
ArrayList<String>();
for (
int
i =
0
; i < toSearch.length; i++) {
if
(toSearch[i].contains(
"dev_mount"
)) {
if
(
new
File(toSearch[i +
2
]).exists()) {
out.add(toSearch[i +
2
]);
}
}
}
return
out;
}
|
Then, when Environment.getexternalstoragestate () returns to "removed" (that is, when the external SD card is not mounted), a list is obtained through the Getdevmountlist () method, The one that can be written in this list is the eMMC storage directory that the system comes with.
Judgment logic:
?
123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
/**
* 获取扩展SD卡存储目录
*
* 如果有外接的SD卡,并且已挂载,则返回这个外置SD卡目录
* 否则:返回内置SD卡目录
*
* @return
*/
public
static
String getExternalSdCardPath() {
if
(SDCardUtils.isMounted()) {
File sdCardFile =
new File(Environment.getExternalStorageDirectory().getAbsolutePath());
return
sdCardFile.getAbsolutePath();
}
String path =
null
;
File sdCardFile =
null
;
ArrayList<String> devMountList = getDevMountList();
for
(String devMount : devMountList) {
File file =
new
File(devMount);
if
(file.isDirectory() && file.canWrite()) {
path = file.getAbsolutePath();
String timeStamp =
new
SimpleDateFormat(
"ddMMyyyy_HHmmss"
).format(
new
Date());
File testWritable =
new
File(path,
"test_"
+ timeStamp);
if
(testWritable.mkdirs()) {
testWritable.delete();
}
else
{
path =
null
;
}
}
}
if
(path !=
null
) {
sdCardFile =
new
File(path);
return
sdCardFile.getAbsolutePath();
}
return
null
;
}
|
Reference:
[1] http://hubingforever.blog.163.com/blog/static/17104057920129198236599/
[2] Http://stackoverflow.com/questions/5694933/find-an-external-sd-card-location
[3] Http://developer.android.com/about/versions/android-3.0.html
Acquisition of internal storage path from Android phone (go)