Android obtains the path and memory of the SD card.

Source: Internet
Author: User

SD card path problems and how to get sdcard memory
Yesterday, after studying the problems of breakthrough storage paths after taking photos, I started to write the storage paths as follows: private string folder = "/sdcard/dcim/camera, different cameras may cause problems in the path. A better way is to use environment to obtain the path. Finally, an example is provided to show you how to obtain the sdcard memory and show it to the user. The content is as follows:
0. Obtain the path of the SD card.
1. describes the environment class.
2. describes the statfs class.
3. Complete example to read sdcard memory

0. Obtain the SD card path
Method 1: Private string folder = "/sdcard/dcim/camera/" (the path for storing pictures in the photo program on the SD card); // write an absolute path, not in favor

Method 2:
Public String getsdpath (){
File sddir = NULL;
Boolean sdcardexist = environment. getexternalstoragestate ()
. Equals (Android. OS. environment. media_mounted); // determines whether the SD card exists.
If (sdcardexist)
{
Sddir = environment. getexternalstoragedirectory (); // obtain the Directory
}
Return sddir. tostring ();

}

Then add a slash to the end and a file name.
String filename = getsdpath () + "/" + name; // The name must exist in the directory.

1. describes the environment class
Environment is a class that provides access to environment variables.
Environment contains constants:
Media_bad_removal
Explanation: getexternalstoragestate () is returned, indicating that the sdcard has been removed before it is detached.
Media_checking
Explanation: getexternalstoragestate () is returned, indicating that the object is checking the disk.
Media_mounted
Explanation: getexternalstoragestate () is returned, indicating whether the object exists and has read/write permissions.
Media_mounted_read_only
Explanation: getexternalstoragestate () is returned, indicating that the object permission is read-only.
Media_nofs
Explanation: getexternalstoragestate () is returned, indicating that the object is blank or unsupported file system is being used.
Media_removed
Explanation: getexternalstoragestate () is returned. If no sdcard exists
Media_shared
Explanation: getexternalstoragestate () is returned. If the sdcard is not installed and shared by USB
Media_unmountable
Explanation: getexternalstoragestate () is returned, and sdcard cannot be installed if it exists but cannot be installed.
Media_unmounted
Explanation: getexternalstoragestate () is returned, and the sdcard has been detached. If the sdcard exists but is not installed
Common Environment methods:
Method: getdatadirectory ()
Explanation: return a file to obtain the android data directory.
Method: getdownloadcachedirectory ()
Explanation: return a file to obtain the android download/cache content directory.
Method: getexternalstoragedirectory ()
Explanation: The returned file gets the external storage directory, that is, sdcard.
Method: getexternalstoragepublicdirectory (string type)
Explanation: The returned file is a high-end public external memory directory to place some types of files.
Method: getexternalstoragestate ()
Explanation: returns a file to obtain the current status of the external storage device.
Method: getrootdirectory ()
Explanation: returns a file to obtain the android root directory.
2. About the statfs class
Statfs is a class that simulates DF commands in Linux to obtain the usage of SD card and mobile phone memory.
Common statfs methods:
Getavailableblocks ()
Explanation: int is returned to obtain the currently available bucket.
Getblockcount ()
Explanation: int is returned to obtain the number of available file systems in the region.
Getblocksize ()
Explanation: int is returned, in bytes. It is a file system.
Getfreeblocks ()
Explanation: int is returned, indicating the remaining space in the block area.
Restat (string path)
Explanation: execute a file system referenced by this object.

3. Complete example to read sdcard memory
The memory card can be plugged in and unplugged on the Android phone at any time. Every action is performed on the action_broadcast that causes the operating system. In this example, the remaining and total capacities of the sdcard are calculated using the method learned above. The Code is as follows:

Package com. Terry;

Import java. Io. file;
Import java. Text. decimalformat;

Import Android. R. Integer;
Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. OS. environment;
Import Android. OS. statfs;
Import Android. View. view;
Import Android. View. View. onclicklistener;
Import Android. widget. Button;
Import Android. widget. progressbar;
Import Android. widget. textview;
Import Android. widget. Toast;

Public
Class getstorageactivity extends activity {
Private button mybutton;
/** Called when the activity is first created .*/
@ Override
Public
Void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Findview ();
Viewholder. mybutton. setonclicklistener (New onclicklistener (){

@ Override
Public
Void onclick (view arg0 ){
// Todo auto-generated method stub
Getsize ();
}
});
}

Void findview (){
Viewholder. mybutton = (button) findviewbyid (R. Id. button01 );
Viewholder. mybar = (progressbar) findviewbyid (R. Id. myprogressbar );
Viewholder. mytextview = (textview) findviewbyid (R. Id. mytextview );
}

Void getsize (){
Viewholder. mytextview. settext ("");
Viewholder. mybar. setprogress (0 );
// Determine whether a memory card is inserted

If (environment. getexternalstoragestate (). Equals (environment. media_mounted )){
File Path = environment. getexternalstoragedirectory ();
// Obtain the path of the sdcard File
Statfs = new statfs (path. getpath ());
// Obtain the block size

Long blocsize = statfs. getblocksize ();
// Obtain the number of blocks

Long totalblocks = statfs. getblockcount ();
// Number of blocks used

Long availablock = statfs. getavailableblocks ();

String [] Total = filesize (totalblocks * blocsize );
String [] availale = filesize (availablock * blocsize );
// Set the maximum value of the progress bar

Int maxvalue = integer. parseint (availale [0])
* Viewholder. mybar. getmax ()/integer. parseint (total [0]);
Viewholder. mybar. setprogress (maxvalue );
String text = "Total:" + total [0] + total [1] + "\ n"
+ "Available:" + availale [0] + availale [1];
Viewholder. mytextview. settext (text );

} Else
If (environment. getexternalstoragestate (). Equals (environment. media_removed )){
Toast. maketext (getstorageactivity. This, "No sdcard", 1000). Show ();
}
}

// Returns an array. subscript 1 indicates the size, and subscript 2 indicates the unit KB/MB.
String [] filesize (Long SIZE ){
String STR = "";
If (size> = 1024 ){
STR = "kb ";
Size/= 1024;
If (size> = 1024 ){
STR = "MB ";
Size/= 1024;
}
}
Decimalformat formatter = new decimalformat ();
Formatter. setgroupingsize (3 );
String result [] = new string [2];
Result [0] = formatter. Format (size );
Result [1] = STR;
Return result;

Note: QQ technology exchange group: add one if you are interested in 108614806.

 

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.