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

其实说是检查更新的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;

}下次注意了~!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值