Android下新建文件的问题

冷伟123 2014-12-15 03:08:04
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()啊?这是什么原因呢?
...全文
59 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
冷伟123 2014-12-15
  • 打赏
  • 举报
回复
3X,明白了,其实我少加了个括号
Birds2018 2014-12-15
  • 打赏
  • 举报
回复
path.mkdirs(); 这个表示没有文件夹就自动创建文件夹,用mkdir 不要 mkdirs

80,471

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧