Android-sdcard path

Source: Internet
Author: User

Dcard 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 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;
}

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.