Android development can be shared with SD card methods available on mobile phones

Source: Internet
Author: User
Tags constructor readline

Today's Android phone models are complex and diverse, resulting in the development process using the official access to the SD card method in the part of the mobile phone does not apply, so need to develop their own packaging, the following is the code, hope to share out, we learn together

/**
* Get the Mobile phone SD card Tool class
* @author WY
*/
public class Sdcardutils {
/*
* Avoid initializations of tool classes
*/
Private Sdcardutils () {
TODO auto-generated Constructor stub
}

/**
* @Title: Getextsdcardpaths
* @Description: To obtain storage paths, the ' a ' and ' theoretically
* The returned value of
* Environment.getexternalstoragedirectory (), namely the
* Primary external storage. It can be the storage of internal
* device, or that of external sdcard. If paths.size () >1,
* Basically, the current device contains two type of storage:
* One is the storage of the device itself, which is
* External SDcard. Additionally, the paths is directory.
* @return list<string>
* @throws IOException
* Get all the SD card paths available on the phone
* @return
*/
public static arraylist<string> Getextsdcardpaths () {
arraylist<string> paths = new arraylist<string> ();
String extfilestatus = Environment.getexternalstoragestate ();
File extfile = Environment.getexternalstoragedirectory ();
if (Extfilestatus.equals (environment.media_mounted) && extfile.exists ()
&& extfile.isdirectory ()) {
Paths.add (Extfile.getabsolutepath ());
}
try {
Obtain executed result of command line code of ' Mount ', to judge
Whether Tfcard exists by the result
Runtime Runtime = Runtime.getruntime ();
Process process = Runtime.exec ("mount");
InputStream is = Process.getinputstream ();
InputStreamReader ISR = new InputStreamReader (IS);
BufferedReader br = new BufferedReader (ISR);
String line = null;
int mountpathindex = 1;
while (line = Br.readline ())!= null) {
Format of sdcard file System:vfat/fuse
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")
|| Line.contains ("Legacy") | | Line.contains ("Data")) {
Continue
}
string[] Parts = Line.split ("");
int length = Parts.length;
if (mountpathindex >= length) {
Continue
}
String Mountpath = Parts[mountpathindex];
if (!mountpath.contains ("/") | | mountpath.contains ("data")
|| Mountpath.contains ("Data")) {
Continue
}
File Mountroot = new file (Mountpath);
if (!mountroot.exists () | |!mountroot.isdirectory ()) {
Continue
}
Boolean EQUALSTOPRIMARYSD = Mountpath.equals (extfile
. GetAbsolutePath ());
if (EQUALSTOPRIMARYSD) {
Continue
}
Paths.add (Mountpath);
}
catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
return paths;
}

/**
* SD card remaining space size
* Get SD Card file path @param path
* @return per MB
*/
public static long Getsdfreesize (String path) {
Statfs SF = new Statfs (path);
Gets the size of a single block of data (Byte)
Long blockSize = Sf.getblocksize ();
Number of free blocks of data
Long freeblocks = Sf.getavailableblocks ();
Return SD card Idle size
Return (Freeblocks * blockSize)/1024/1024; //
}

/**
* SD Card Total capacity
* Get SD Card file path @param path
* @return per MB
*/
public static long Getsdallsize (String path) {
Statfs SF = new Statfs (path);
Gets the size of a single block of data (Byte)
Long blockSize = Sf.getblocksize ();
Get all blocks of data
Long allblocks = Sf.getblockcount ();
Back to SD card size
return allblocks * blockSize; Unit byte
Return (Allblocks * blockSize)/1024; Units kb
Return (Allblocks * blockSize)/1024/1024;
}

/**
* Determine if the current memory card is available
* @param mcontext
* @return
*/
public static Final Boolean isexist (String sdpath) {
arraylist<string> list = Getextsdcardpaths ();
for (int i = 0; i < list.size (); i++) {
if (List.contains (Sdpath)) {
return true;
}
}
return false;
}
}


the method of obtaining the external SD card (TF card) of Android mobile phone

An external SD card on an Android phone, at first, when Android was born a few years ago, when the cell phone storage was very limited, unlike now everywhere 16G, 32G and 64G of storage, so the mobile phone some manufacturers to allow the insertion of external SD card, The card is still in the extended part of the phone. Later, with the development of mobile phones and the increase in storage capacity, this external SD card, gradually become a part of the mobile phone, no longer allowed to be tall and straight, of course, there are still some mobile phones allow storage to expand, such as Samsung.

The expanded memory card, now called the TF card, is not supported by all handsets, but sometimes some of the exotic needs are first stored in the TF card, which calls for a developer to check if the card exists and is available. And because this is a mobile phone manufacturer can expand, customizable part, all the mobile phones produced by different manufacturers, and the same manufacturer of different models of mobile phones, TF card location is very large, there is no uniform name or location. So this is a more difficult part, but it's okay that Android is open source, and we can use the runtime to determine if the phone has a TF card and if the TF card is available.

The following method can be used to obtain the mobile phone can be stored, including SD card, TF card, etc., to the multiple memory card matching, detailed code is as follows:

1 public class Sdcardscanner {
2/*
3 * Avoid initializations of tool classes
4 */
5 Private Sdcardscanner () {
6}
7
8/**
9 * @Title: getextsdcardpaths
@Description: To obtain storage paths, the ' a ' and ' theoretically
* The returned value of
* Environment.getexternalstoragedirectory (), namely the
Primary external storage. It can be the storage of internal
device, or that of external sdcard. If paths.size () >1,
* Basically, the current device contains two type of storage:
* One is the storage of the device itself, which is
* External SDcard. Additionally, the paths is directory.
* @return list<string>
* @throws IOException
20 */
public static list<string> Getextsdcardpaths () {
list<string> paths = new arraylist<string> ();
A String extfilestatus = Environment.getexternalstoragestate ();
File extfile = Environment.getexternalstoragedirectory ();
if (Extfilestatus.endswith (environment.media_unmounted)
&& extfile.exists () && extfile.isdirectory ()
&& Extfile.canwrite ()) {
Paths.add (Extfile.getabsolutepath ());
29}
try {
//obtain executed result of command line code of ' Mount ' to judge
//Whether Tfcard exists by the result
Runtime Runtime = Runtime.getruntime ();
The process process = runtime.exec (' Mount ');
InputStream is = Process.getinputstream ();
InputStreamReader ISR = new InputStreamReader (IS);
Panax Notoginseng BufferedReader br = new BufferedReader (ISR);
String line = null;
int mountpathindex = 1;
while (line = Br.readline ())!= null) {
A/format of sdcard file System:vfat/fuse
if (!line.contains (' fat ') &&!line.contains (' fuse ') &&!line
Contains (' Storage ')
44 | | Line.contains (' secure ')
45 | | Line.contains (' ASEC ')
46 | | Line.contains (' firmware ')
47 | | Line.contains (' Shell ')
48 | | Line.contains (' Obb ')
49 | | Line.contains (' legacy ') | | Line.contains (' data ')) {
Continue;
51}
string[] Parts = Line.split (");
int length = Parts.length;
The IF (mountpathindex >= length) {
Continue;
56}
A String Mountpath = Parts[mountpathindex];
!mountpath.contains ('/') | | mountpath.contains (' data ')
59 | | Mountpath.contains (' Data ')) {
Continue;
61}
The file Mountroot = new file (Mountpath);
if (!mountroot.exists () | |!mountroot.isdirectory ()
64 | | !mountroot.canwrite ()) {
Continue;
66}
Boolean EQUALSTOPRIMARYSD = Mountpath.equals (extfile
GetAbsolutePath ());
if (EQUALSTOPRIMARYSD) {
Continue;
71}
Paths.add (Mountpath);
73}
IOException e} catch (d) {
M//TODO auto-generated catch block
E.printstacktrace ();
77}
Paths return;
79}
80}


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

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

Then using the runtime, the command line function ' Mount ' to get all the storage location, and the return of the results of the SD card or TF card lookup.

Finally, the location of all the different cards that can be used for storage is returned and saved with a list. Since not all mobile phones support TF cards, 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 that is necessary, Paths.get (0) is definitely the location of the external SD card because it is primary external storage.

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.