sdcard操作,一定要记得加上权限
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> 允许挂载和反挂载文件系统可移动存储
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <!-- 在SDCard中创建与删除文件权限 -->
不然创建目录操作会出错,
至于这个权限
//文件夹目录"/sdcard/FirstFolder/SecondFolder",多级目录必须逐一创建
//sdcard不存在,只能将目录创建在“/data/data/[your path]/”中
public static String FirstFolder="MyMusics";//一级目录
public static String SecondFolder="MyLrc";//二级目录
/*ALBUM_PATH取得机器的SD卡位置,File.separator为分隔符“/”*/
private final static String ALBUM_PATH=Environment.getExternalStorageDirectory()+File.separator+FirstFolder+File.separator;
private final static String Second_PATH=ALBUM_PATH+SecondFolder+File.separator;
private final static String NO_SDCARD_ALBUM_PATH="/data/data/" + "com.asl.mymusic"+File.separator+FirstFolder+File.separator;
private final static String NO_SDCARD_Second_PATH=NO_SDCARD_ALBUM_PATH+SecondFolder+File.separator;
public void oncreatefile(){
boolean sdCardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
System.out.println("file album path is "+ ALBUM_PATH);
if (!sdCardExist) {
//sdcard 不存在
System.out.println("sdcard is not exist, FILE IS " + File.separator+FirstFolder+File.separator);
File dirFirstFile = new File(NO_SDCARD_ALBUM_PATH);
if (!dirFirstFile.exists()) {
dirFirstFile.mkdir();
}
File dirSecondFile = new File(NO_SDCARD_Second_PATH);
if (!dirSecondFile.exists()) {
dirSecondFile.mkdir();
}
System.out.println(dirSecondFile.getAbsolutePath()+"is "+dirSecondFile.exists());
}else {
//sdcard存在
File dirFirstFile = new File(ALBUM_PATH);
if (!dirFirstFile.exists()) {
dirFirstFile.mkdir();
}
File dirSecondFile = new File(Second_PATH);
if (!dirSecondFile.exists()) {
dirSecondFile.mkdir();
}
System.out.println(dirSecondFile.getAbsolutePath()+"is "+dirSecondFile.exists());
}
}
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> 允许挂载和反挂载文件系统可移动存储
貌似不加,也可以
代码如下