android app检查更新遇到的一个小bug
2016-01-06 16:48
525 查看
其实说是检查更新的bug,不如说是对android的权限不太了解,对DownloadManager的方法不太熟。好吧,还是我见识少。
在Java中这么写:
但是在android中这么写:
第二个会mkdirs方法会返回false。
在下载更新的时候会有这么一段:
17 DownloadManager的源码
这是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;
}下次注意了~!
在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;
}下次注意了~!
相关文章推荐
- APP切图详细规范终极指南
- StringUtil
- iOS的Bundle资源束制作
- unity 开发中的优化准则
- Basic Operators Of Swift 2.1
- iOS企业级用户版本更新
- MPAndroidChart
- swfobject.js 详细解说
- Android入门学习
- IOS中.a库的封装和使用以及遇到i386的问题
- 如何使用Retrofit写一个Android的REST客户端的小教程
- Unity给力插件之ShaderForge(三)
- Application Installation Failed
- Android初学习 - AsyncTask的一些介绍II
- Android手势锁屏界面
- Android应用开发基础篇(16)-----ScaleGestureDetector(缩放手势检测)
- Android初学习 - AsyncTask的一些介绍I
- android使用5.0的toolbar,自定义布局参考地址
- 利用反射实现对sqlite3数据库的crud(增删改查)操作的一个baseAndroidDao封装,安卓开发中
- Android屏幕适配全攻略(最权威的官方适配指导)