The method of acquiring the internal storage path of mobile phone with Android programming _android

Source: Internet
Author: User
Tags emmc storage


This article describes the Android programming to achieve a mobile phone with the internal storage path to obtain the method. Share to everyone for your reference, specific as follows:



I have a Android phone, model is U930HD, the mobile phone did not insert an external SD card (that is, micro SD cards, formerly known as Trans-flash Card (TF), 2004 officially renamed as Micro SD), But the fuselage has a built-in memory card (i.e. EMMC storage, size of 2G).



I put this phone into the computer with data cable, will also see the letter, by installing "R.E Manager" and other file management applications, can also manage files, and can see the storage of the Mount directory is:/mnt/sdcard2



However, I print environment.getexternalstoragestate (), but return to "removed";



What's going on? Obviously the phone itself with the built-in SD card, but why prompted such a message?



I tried to print it again. Environment.getexternalstoragedirectory (), return: "/mnt/sdcard"



It seems to explain that on my phone, call Environment.getexternalstoragedirectory (), the returned storage directory is not the system's built-in SD card directory.



I switched to a Sony l39u, a MOTO G, the directory that calls Environment.getexternalstoragedirectory () is the system's built-in SD card directory.



On different devices, the return value of the call getExternalStorageDirectory () is not the same. Check the Android document to find out why, the original method returned by the current device manufacturers think of "external storage", it is possible to return to the external SD card directory (Micro SD cards), may also return the built-in storage target (EMMC).



To sum up:



Some mobile phones will emmc storage mount to/MNT/EXTERNAL_SD,/mnt/sdcard2 and other nodes, and the external SD card mounted to the Environment.getexternalstoragedirectory () node.
At this point, the call to Environment.getexternalstoragedirectory () returns the path to the external SD.



And the other part of the phone directly eMMC storage mount in Environment.getexternalstoragedirectory () This node, and the real external SD card mount to/MNT/EXTERNAL_SD,/mnt/sdcard2 and other nodes.
At this point, the call to Environment.getexternalstoragedirectory () returns the path to the built-in SD.



This can be explained that there is no external SD card in the case, on the phone, call



Print environment.getexternalstoragestate (), but return to "removed", on the Sony, MOTO G on the return: "Mounted"



The reason is already known, but how to get to the specific path of the built-in EMMC storage when there is no external SD card?



For example, my phone, since using environment.getexternalstoragedirectory () to get to 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 do I get the path to the eMMC store?



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:


/**
* Traverse the "system/etc/vold.fstab" file to obtain all of the Android mount point information
* 
* @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), the Getdevmountlist () method gets a list, One of the things that can be written in this list is the eMMC storage directory that comes with the system itself.



Judgment logic:


/** * Obtain extended SD card Storage Directory * * If an external SD card is installed and mounted, return the external SD card directory * Otherwise: return the built-in SD card directory * * @return/Publi C Static String Getexternalsdcardpath () {if (sdcardutils.ismounted ()) {File Sdcardfile = new File (Environment.getex
   Ternalstoragedirectory (). 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; }


I hope this article will help you with your Android programming.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.