检查sdcard文件的状态,是否sdcard是否可用,sdcard是否可写。
protected void checkSDcardStatus(){
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
mExternalStorageAvailable = mExternalStorageWriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
} else {
mExternalStorageAvailable = mExternalStorageWriteable = false;
}
}
文件路径
//获取sdcard根目录
File sdcardDir = Environment.getExternalStorageDirectory();
String path = sdcardDir.getParent() + java.io.File.separator
+ sdcardDir.getName();
System.out.println("sdcardDir=" + sdcardDir.getAbsolutePath());
System.out.println("sdcardDirParent=" + sdcardDir.getParent());
System.out.println("sdcardDirName=" + sdcardDir.getName());
System.out.println("path=" + path);
//【推荐使用】java.io.File.separator表示文件路径的分隔符,由java根据系统选择是\r\n,\r还是\n
String filePath = path + java.io.File.separator + "My Documents"
+ java.io.File.separator + "My Recordings";
System.out.println("filepath=" + filePath);
File myRecAudioFile = new File(filePath, "Kalimba.mp3");
// 检查文件是否存在
System.out.println("isExist=" + myRecAudioFile.exists());
System.out.println("audioFileAbsPath="
+ myRecAudioFile.getAbsolutePath());
try {
//【推荐使用】带.和..的形式的文件名也解析出来
System.out.println("audioFileCanPath="
+ myRecAudioFile.getCanonicalPath());
} catch (IOException e) {
e.printStackTrace();
}
09-29 11:59:00.111: INFO/System.out(26594): sdcardDir=/mnt/sdcard
09-29 11:59:00.111: INFO/System.out(26594): sdcardDirParent=/mnt
09-29 11:59:00.111: INFO/System.out(26594): sdcardDirName=sdcard
09-29 11:59:00.111: INFO/System.out(26594): path=/mnt/sdcard
09-29 11:59:00.111: INFO/System.out(26594): filepath=/mnt/sdcard/My Documents/My Recordings
09-29 11:59:00.111: INFO/System.out(26594): isExist=true
09-29 11:59:00.111: INFO/System.out(26594): audioFileAbsPath=/mnt/sdcard/My Documents/My Recordings/Kalimba.mp3
09-29 11:59:00.111: INFO/System.out(26594): audioFileCanPath=/mnt/sdcard/My Documents/My Recordings/Kalimba.mp3