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(); }
这回结果符合预期。
本文探讨了在Android系统中使用不同方法创建文件夹的过程,并对比了mkdir、mkdirs及getParentFile方法的效果,指出了正确创建目标文件夹的方式。
917

被折叠的 条评论
为什么被折叠?



