Android are several ways to get the SD card path / memory

The following describes started, first get the root directory of the SD card method:

Add permissions:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
1
2
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
//为真则SD卡已装入,
sdCardExist=Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);

}

IF (sdCardExist)
{
sdDir = Environment.getExternalStorageDirectory (); // get with the directory
// Find the SD card root path
sdDir.toString ();
Log.e ( "main", "root directory path obtained:" + sdDir) ;
}
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
printed Log:

Bloggers open the phone's file manager first came to the page where is this path, but the path will not be displayed on a mobile phone

 

 


In addition a more flexible method of obtaining external path:

Directory_doc = Environment.getExternalStoragePublicDirectory File (Environment DIRECTORY_DOCUMENTS.);
// This method requires the use of public directories such as the type of incoming Environment.DIRECTORY_DOCUMENTS
// View Public directory path document files
Log.e ( "main", "get the public directory : "+ directory_doc);
. 1
2
. 3
. 4
another method to get the path;

The method described
DIRECTORY_DOCUMENTS standard file directory, users can be custom-created. Directory Place in Which to Standard Documents that have have been at The the Created by the User.
DIRECTORY_DOWNLOADS download directory. Standard Directory Place in Which to the Downloaded Files that have have been at The by the User.
DIRECTORY_MOVIES video catalog .Standard directory in which to place movies that are available to the the user.
DIRECTORY_MUSIC music catalog directory in Which to Place .Standard the any Audio Files Should bE in that at the Regular at the List of Music for the user.
DIRECTORY_PICTURES users can get a picture directory .Standard directory in which to place pictures that are available to the user.
more: https: //developer.android.com/reference/android/os/Environment

 

 


When you have multiple external memory cards:

File[] files;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
files = getExternalFilesDirs(Environment.MEDIA_MOUNTED);
for(File file:files){
Log.e("main", "得到的全部外存:" +String.valueOf(file));


// all external storage calendar will
}
}
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10

Because bloggers here only a phone sd card, so only print one line path
1
2

 

Get memory:

Internal getFilesDir = File ();
Log.e ( "main", "Path Memory:" Internal +);
---------------------

Guess you like

Origin www.cnblogs.com/ly570/p/11014131.html