Section1 mkdir
if (Environment.MEDIA_MOUNTED.equals( Environment.getExternalStorageState())){ String path = Environment.getExternalStorageDirectory().getAbsolutePath(); path=path+File.separator+"1ATesthaha"+File.separator+"tempImage.png"; Toast.makeText(this,path,Toast.LENGTH_LONG).show(); File file = new File(path); file.mkdir(); }
检查文件发现没有创建成功
Section2 mkdirs
我们做一修改
if (Environment.MEDIA_MOUNTED.equals( Environment.getExternalStorageState())){ String path = Environment.getExternalStorageDirectory().getAbsolutePath(); path=path+File.separator+"1ATesthahab"+File.separator+"tempImage.png"; Toast.makeText(this,path,Toast.LENGTH_LONG).show(); File file = new File(path); file.mkdirs(); }
这次创建成功了,但是结果不是我们想要的
tempImage.png成了文件夹了
Section3 getParentFile
if (Environment.MEDIA_MOUNTED.equals( Environment.getExternalStorageState())){ String path = Environment.getExternalStorageDirectory().getAbsolutePath(); path=path+File.separator+"1ATesthahac"+File.separator+"tempImage.png"; Toast.makeText(this,path,Toast.LENGTH_LONG).show(); File file = new File(path); file.getParentFile().mkdirs(); }
这回结果符合预期。