How to obtain an Android phone external SD card (TF card)

Source: Internet
Author: User

The external SD card on the Android phone, at first, in the first few years of Android, when the storage of the phone is very limited, not like now everywhere 16G, 32G and 64G storage, so at that time the handset some manufacturers allow to insert the external SD card, At this point the card is still in the extended portion of the phone. Later, with the development of mobile phones and the increase in storage capacity, this external SD card, gradually became part of the phone, no longer allowed to be upright, of course, there are still some mobile phones allow the expansion of storage, such as Samsung.

The expansion of the memory card, now called the TF card, and not all the phones support it, but sometimes some wonderful needs to be stored in the preferred TF card, it is called to ask the developer to check whether the card exists, whether it is available. Because this is the mobile phone manufacturers can expand, customizable parts, all the different manufacturers of mobile phones, and the same manufacturer of different models of mobile phones, TF card location is very large, and does not have a unified name or location. So this is a more difficult part, but fortunately Android is open source, we can run to determine whether the phone has a TF card, and whether the TF card is available.

The following method can be obtained from the phone can be stored, including SD card, TF card, etc., the multi-memory card matching, detailed code as follows:

1  Public classSdcardscanner {2     /*3 * Avoid initializations of tool classes4      */5     PrivateSdcardscanner () {6     }7 8     /**9 * @Title: getextsdcardpathsTen * @Description: To obtain storage paths, the first path is theoretically One * The returned value of A * Environment.getexternalstoragedirectory (), namely the - * Primary external storage. It can be the storage of internal - * device, or that of external sdcard. If paths.size () >1, the * Basically, the current device contains the type of storage: - * One is the storage of the device itself, and one is the - * External SDcard. Additionally, the paths is directory. -      * @returnlist<string> +      * @throwsIOException -      */ +      Public StaticList<string>getextsdcardpaths () { Alist<string> paths =NewArraylist<string>(); atString Extfilestatus =environment.getexternalstoragestate (); -File Extfile =environment.getexternalstoragedirectory (); -         if(Extfilestatus.endswith (environment.media_unmounted) -&& extfile.exists () &&extfile.isdirectory () -&&Extfile.canwrite ()) { - Paths.add (Extfile.getabsolutepath ()); in         } -         Try { to             //obtain executed result of command line code of ' Mount ', to judge +             //whether Tfcard exists by the result -Runtime runtime =runtime.getruntime (); theProcess process = Runtime.exec ("Mount"); *InputStream is =Process.getinputstream (); $InputStreamReader ISR =NewInputStreamReader (IS);Panax NotoginsengBufferedReader br =NewBufferedReader (ISR); -String line =NULL; the             intMountpathindex = 1; +              while(line = Br.readline ())! =NULL) { A                 //format of sdcard file System:vfat/fuse the                 if((!line.contains ("fat") &&!line.contains ("fuse") &&! Line +. Contains ("Storage")) -|| Line.contains ("Secure") $|| Line.contains ("ASEC") $|| Line.contains ("firmware") -|| Line.contains ("Shell") -|| Line.contains ("Obb") the|| Line.contains ("Legacy") | | Line.contains ("Data")) { -                     Continue;Wuyi                 } thestring[] Parts = Line.split (""); -                 intLength =parts.length; Wu                 if(Mountpathindex >=length) { -                     Continue; About                 } $String Mountpath =Parts[mountpathindex]; -                 if(!mountpath.contains ("/") | | mountpath.contains ("data")) -|| Mountpath.contains ("Data")) { -                     Continue; A                 } +File Mountroot =NewFile (mountpath); the                 if(!mountroot.exists () | | |mountroot.isdirectory () -|| !Mountroot.canwrite ()) { $                     Continue; the                 } the                 BooleanEQUALSTOPRIMARYSD =mountpath.equals (Extfile the . GetAbsolutePath ()); the                 if(EQUALSTOPRIMARYSD) { -                     Continue; in                 } the Paths.add (mountpath); the             } About}Catch(IOException e) { the             //TODO auto-generated Catch block the e.printstacktrace (); the         } +         returnpaths; -     } the}
View Code

First, I wrote it as a tool class, so I declare a private constructor to prevent the class from being instantiated.

Then, first get the Android standard part of the external SD card, if it is available.

Then using the runtime, through the command line function "mount" to obtain all the storage location, and the results of the return of the SD card or TF card lookup.

Finally returns the location of all the different cards available for storage, saved with a list. Since not all phones support TF card, so this list contains not many paths, only one SD card phone will return only one path, multiple available storage locations will return multiple paths.

But one thing is necessary, Paths.get (0) must be the location of the external SD card because it is primary external storage.

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.