当前位置: 代码迷 >> Android >> Android下新建文件的有关问题
  详细解决方案

Android下新建文件的有关问题

热度:66   发布时间:2016-04-28 03:21:00.0
Android下新建文件的问题
Android下碰到一个比较诡异的问题: 通过一下两种方式居然得到不同的效果: 

第一种: String userIconPath = FileManager.getUserHeadPath(config.getSchoolId(),config.getUserTel()+"/"+config.getUserId()+"userIcon.png");
if(new File(userIconPath).exists())
{
Log.e("头像未被设置",userIconPath);
headIcon.setImageBitmap(FileManager.getBitmap(context));
}
第一种的效果是:新建了一个名为“3userIcon.png”的文件夹.

第二种:String userIconPath = FileManager.getUserHeadPath(config.getSchoolId(), config.getUserTel());
String iconPath = userIconPath+"/"+config.getUserId()+"userIcon.png";
//服务端请求头像的数据,本地存在从本地取,否则,从服务器下载下来再再本地取
if(new File(iconPath).exists())
{
Log.e("头像未被设置",userIconPath);
headIcon.setImageBitmap(FileManager.getBitmap(context));
}
第二种的效果是并不新建“3userIcon.png”,只有在new File(iconPath).createNewFile();才新建“3userIcon.png”的图片,注意:是图片

可能和FileManager.getUserHeadPath这个方法有关,但是我找不到原因

public static final String getUserHeadPath(int schoolId, String account) {
        if (hasSdcard()) {
            File path = new File(FileManager.USER_HEAD_BASE + "/" + schoolId
                    + "_" + account);
            if (!path.exists()) {
                path.mkdirs();
            }
            return FileManager.USER_HEAD_BASE + "/" + schoolId + "_" + account;
        }
        return null;
    }

是不是我可以理解成,假如把FileManager.getUserHeadPath和“/3userIcon.png”放到一起然后封装到File里以后,因为前者可以创建文件夹,封装以后3userIcon.png也自然被创建成文件夹了,但是我并没有mkdirs()啊?这是什么原因呢?
------解决思路----------------------
path.mkdirs(); 这个表示没有文件夹就自动创建文件夹,用mkdir 不要 mkdirs
  相关解决方案