您的位置:首页 > 移动开发

android app检查更新遇到的一个小bug

2016-01-06 16:48 525 查看
其实说是检查更新的bug,不如说是对android的权限不太了解,对DownloadManager的方法不太熟。好吧,还是我见识少。

在Java中这么写:

File file = new File("d://t1/t2");
if(file.exists()&&file.isDirectory()){

}else{
System.out.println(file.mkdirs());
}
File file2 = new File("t1/t2");
if(file2.exists()&&file2.isDirectory()){

}else{
System.out.println(file2.mkdirs());
}
正常情况下是没有问题的,会在d盘根目录下创建t1文件夹,t1中创建t2文件夹;会在项目的根目录下创建t1文件夹,t1中创建t2文件夹。

但是在android中这么写:

File skRoot = Environment.getExternalStorageDirectory();
File file = new File(skRoot+"/t1/t2");
if(file.exists()&&file.isDirectory()){

}else{
Log.i("===", file.mkdirs()+"");//true
}
File file2 = new File("/t3/t4");
if(file2.exists()&&file2.isDirectory()){

}else{
Log.i("===", file2.mkdirs() + "");//false
}


第二个会mkdirs方法会返回false。

在下载更新的时候会有这么一段:

DownloadManager dManager = (DownloadManager) context
.getSystemService(Context.DOWNLOAD_SERVICE);
Uri uri = Uri.parse(mUpgradeUrl);
DownloadManager.Request request = new DownloadManager.Request(uri);
// 设置下载路径和文件名
// 判断下权限
File skRoot = Environment.getExternalStorageDirectory();
String downloadPath = skRoot + "/Download";
File fileDir = new File(downloadPath);
if (Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)) {
if (fileDir.exists() && fileDir.isDirectory()) {
} else {
fileDir.mkdirs();
}
}
request.setDestinationInExternalPublicDir(fileDir.getName(), "XXX");
request.setDescription("XXX新版本下载");
request.setDestinationInExternalPublicDir(fileDir.getName(), "XXX");
这一句开始是这么写的

request.setDestinationInExternalPublicDir(downloadPath, "XXX");
但是测试发现升级更新失败了。why?查看setDestinationInexternalPublicDir()这个方法发现,其实上面检查创建文件夹的过程在这个方法内部其实已经做了,所以没有必要,而且这个方法会用第一个参数作为路径创建文件夹,比如传递的参数是t1/t2/t3/Download,那么会在sd卡的根目录下创建t1,在t1中创建t2,依次类推。所以这个方法需要的是一个路径,但不是绝对路径。比如这样storage/sdcard/0/Download。有点啰嗦,再回到问题,为什么会创建路径出错了呢?storage/sdcard/0/Download这个虽然是绝对路径但是也可以当做相对路径来使用嘛。思路完全不对啊!然后查看了两个版本的api
fileDir.mkdirs();//对,就是这货!
 这是api
17 DownloadManager的源码
public Request setDestinationInExternalPublicDir(String dirType, String subPath) {
File file = Environment.getExternalStoragePublicDirectory(dirType);
if (file.exists()) {
if (!file.isDirectory()) {
throw new IllegalStateException(file.getAbsolutePath() +
" already exists and is not a directory");
}
} else {
if (!file.mkdir()) {
throw new IllegalStateException("Unable to create directory: "+
file.getAbsolutePath());
}
}
setDestinationFromBase(file, subPath);
return this;
}

这是20的:

public Request setDestinationInExternalPublicDir(String dirType, String subPath) {
File file = Environment.getExternalStoragePublicDirectory(dirType);
if (file == null) {
throw new IllegalStateException("Failed to get external storage public directory");
} else if (file.exists()) {
if (!file.isDirectory()) {
throw new IllegalStateException(file.getAbsolutePath() +
" already exists and is not a directory");
}
} else {
if (!file.mkdirs()) {
throw new IllegalStateException("Unable to create directory: "+
file.getAbsolutePath());
}
}
setDestinationFromBase(file, subPath);
return this;
}下次注意了~!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: