Android Get Path directory method and determine if directory exists, create directory

Source: Internet
Author: User

Environment Common methods:

* Method: Getdatadirectory ()
Explanation: Return File to get the Android data directory.
* Method: Getdownloadcachedirectory ()
Explanation: Return File to get the Android download/cache content directory.
* Method: getExternalStorageDirectory ()
Explanation: Return File, Get External storage directory is SDcard
* Method: Getexternalstoragepublicdirectory (String type)
Explanation: Return file, take a high-end common external memory directory to place certain types of files
* Method: Getexternalstoragestate ()
Explanation: Returns File to get the current state of the external storage device
* Method: Getrootdirectory ()
Explanation: Return File, get the root directory of Android


public void Getsdpath () {
File sddir = null;
File sdDir1 = null;
File sdDir2 = null;
Boolean sdcardexist = Environment.getexternalstoragestate ()
. Equals (Android.os.Environment.MEDIA_MOUNTED); Determine if the SD card exists
if (sdcardexist)
{
Sddir = Environment.getexternalstoragedirectory ();//Get with Directory
SdDir1 = Environment.getdatadirectory ();
SdDir2 =environment.getrootdirectory ();
}
System.out.println ("getExternalStorageDirectory ():" +sddir.tostring ());
System.out.println ("Getdatadirectory ():" +sddir1.tostring ());
System.out.println ("Getrootdirectory ():" +sddir2.tostring ());
}



Determine if a file (folder) exists under a path

public class Isexist {
public static void Main (string[] args) {
Isexist ("E://12");
}
/**
*
* Path @param path folder
*/
public static void Isexist (String path) {
File File = new file (path);
Determine if a folder exists and create a folder if it does not exist
if (!file.exists ()) {
File.mkdir ();
}
}
}

1. Create a folder on the SD card
public class make extends Activity {
/** called when the activity is first created. */
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
File sd=environment.getexternalstoragedirectory ();
String Path=sd.getpath () + "/notes";
File File=new file (path);
if (!file.exists ())
File.mkdir ();

}

Android Get Path directory method and determine if directory exists, create directory

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.