【我的Android进阶之旅】 解决bug: Expected file scheme in URI: content://downloads/my_downloads/12
一、错误描述
今天测试MM用HTC手机测试某个模块的时候crash了,抓log后发现是使用DownloadManager下载apk安装包然后自动安装的时候,抛了异常:java.lang.IllegalArgumentException: Expected file scheme in URI: content://downloads/my_downloads/12
具体crash错误信息如下所示:
11-30 09:24:21.933 28279 28279 E AndroidRuntime: FATAL EXCEPTION: main
11-30 09:24:21.933 28279 28279 E AndroidRuntime: Process: com.xtc.watch, PID: 28279
11-30 09:24:21.933 28279 28279 E AndroidRuntime: java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.DOWNLOAD_COMPLETE flg=0x10 pkg=com.xtc.watch (has extras) } in
com.xtc.watch.service.compatible.VersionCompatibleUpdateService$DownloadCompleteReceiver@33fa7ec
11-30 09:24:21.933 28279 28279 E AndroidRuntime: at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:934)
11-30 09:24:21.933 28279 28279 E AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
11-30 09:24:21.933 28279 28279 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
11-30 09:24:21.933 28279 28279 E AndroidRuntime: at android.os.Looper.loop(Looper.java:168)
11-30 09:24:21.933 28279 28279 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5885)
11-30 09:24:21.933 28279 28279 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
11-30 09:24:21.933 28279 28279 E AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
11-30 09:24:21.933 28279 28279 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
11-30 09:24:21.933 28279 28279 E AndroidRuntime: Caused by: java.lang.IllegalArgumentException: Expected file scheme in URI: content://downloads/my_downloads/12
11-30 09:24:21.933 28279 28279 E AndroidRuntime: at java.io.File.checkURI(File.java:223)
11-30 09:24:21.933 28279 28279 E AndroidRuntime: at java.io.File.<init>(File.java:175)
11-30 09:24:21.933 28279 28279 E AndroidRuntime: at com.xtc.watch.service.compatible.VersionCompatibleUpdateService.a(Unknown Source)
11-30 09:24:21.933 28279 28279 E AndroidRuntime: at com.xtc.watch.service.compatible.VersionCompatibleUpdateService.a(Unknown Source)
11-30 09:24:21.933 28279 28279 E AndroidRuntime: at com.xtc.watch.service.compatible.VersionCompatibleUpdateService$DownloadCompleteReceiver.onReceive(Unknown Source)
11-30 09:24:21.933 28279 28279 E AndroidRuntime: at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:915)
11-30 09:24:21.933 28279 28279 E AndroidRuntime: ... 7 more
11-30 09:24:21.939 1785 3503 E ActivityManager: App crashed! Process: com.xtc.watch
二、错误分析
进入File.java查看代码,
/**
* Constructs a new File using the path of the specified URI. {@code uri}
* needs to be an absolute and hierarchical Unified Resource Identifier with
* file scheme and non-empty path component, but with undefined authority,
* query or fragment components.
*
* @param uri
* the Unified Resource Identifier that is used to construct this
* file.
* @throws IllegalArgumentException
* if {@code uri} does not comply with the conditions above.
* @see #toURI
* @see java.net.URI
*/
public File(URI uri) {
// check pre-conditions
checkURI(uri);
this.path = fixSlashes(uri.getPath());
}
checkURI(URI uri)的源代码如下:
private static void checkURI(URI uri) {
if (!uri.isAbsolute()) {
throw new IllegalArgumentException("URI is not absolute: " + uri);
} else if (!uri.getRawSchemeSpecificPart().startsWith("/")) {
throw new IllegalArgumentException("URI is not hierarchical: " + uri);
}
if (!"file".equals(uri.getScheme())) {
throw new IllegalArgumentException("Expected file scheme in URI: " + uri);
}
String rawPath = uri.getRawPath();
if (rawPath == null || rawPath.isEmpty()) {
throw new IllegalArgumentException("Expected non-empty path in URI: " + uri);
}
if (uri.getRawAuthority() != null) {
throw new IllegalArgumentException("Found authority in URI: " + uri);
}
if (uri.getRawQuery() != null) {
throw new IllegalArgumentException("Found query in URI: " + uri);
}
if (uri.getRawFragment() != null) {
throw new IllegalArgumentException("Found fragment in URI: " + uri);
}
}
原来就是下面这一段代码抛出的异常,如果uri不是以file开头的就抛异常IllegalArgumentException(“Expected file scheme in URI: ” + uri),
if (!"file".equals(uri.getScheme())) {
throw new IllegalArgumentException("Expected file scheme in URI: " + uri);
}
而我们的uri 是通过下面代码获取而来的
Uri downloadFileUri = manager.getUriForDownloadedFile(downId);
File file = new File(new URI(downloadFileUri.toString()));
得到的uri信息为
content://downloads/my_downloads/12
因此就抛了该异常出来。
getUriForDownloadedFile(long id)源代码如下:
/**
* Returns the {@link Uri} of the given downloaded file id, if the file is
* downloaded successfully. Otherwise, null is returned.
*<p>
* If the specified downloaded file is in external storage (for example, /sdcard dir),
* then it is assumed to be safe for anyone to read and the returned {@link Uri} corresponds
* to the filepath on sdcard.
*
* @param id the id of the downloaded file.
* @return the {@link Uri} of the given downloaded file id, if download was successful. null
* otherwise.
*/
public Uri getUriForDownloadedFile(long id) {
// to check if the file is in cache, get its destination from the database
Query query = new Query().setFilterById(id);
Cursor cursor = null;
try {
cursor = query(query);
if (cursor == null) {
return null;
}
if (cursor.moveToFirst()) {
int status = cursor.getInt(cursor.getColumnIndexOrThrow(COLUMN_STATUS));
if (DownloadManager.STATUS_SUCCESSFUL == status) {
int indx = cursor.getColumnIndexOrThrow(
Downloads.Impl.COLUMN_DESTINATION);
int destination = cursor.getInt(indx);
// TODO: if we ever add API to DownloadManager to let the caller specify
// non-external storage for a downloaded file, then the following code
// should also check for that destination.
if (destination == Downloads.Impl.DESTINATION_CACHE_PARTITION ||
destination == Downloads.Impl.DESTINATION_SYSTEMCACHE_PARTITION ||
destination == Downloads.Impl.DESTINATION_CACHE_PARTITION_NOROAMING ||
destination == Downloads.Impl.DESTINATION_CACHE_PARTITION_PURGEABLE) {
// return private uri
return ContentUris.withAppendedId(Downloads.Impl.CONTENT_URI, id);
} else {
// return public uri
String path = cursor.getString(
cursor.getColumnIndexOrThrow(COLUMN_LOCAL_FILENAME));
return Uri.fromFile(new File(path));
}
}
}
} finally {
if (cursor != null) {
cursor.close();
}
}
// downloaded file not found or its status is not 'successfully completed'
return null;
}
三、错误解决
将之前查找下载好的apk文件的代码
Uri downloadFileUri = manager.getUriForDownloadedFile(downId);
File file = new File(new URI(downloadFileUri.toString()));
改为通过downId来查询DownloadManager下载的数据库的记录,然后查找本地文件的路径,如下所示:
Cursor c = manager.query(new DownloadManager.Query().setFilterById(downId));
if(c != null){
c.moveToFirst();
int fileNameIdx = c.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME);
String fileName = c.getString(fileNameIdx);
File file = new File(fileName);
LogUtil.d(TAG, "安装文件:" + file.getAbsolutePath());
c.close();
}
安装apk的代码由
//自动安装apk
Intent install = new Intent(Intent.ACTION_VIEW);
install.setDataAndType(downloadFileUri, "application/vnd.android.package-archive");
install.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(install);
改为
//自动安装apk
Intent install = new Intent(Intent.ACTION_VIEW);
//install.setDataAndType(downloadFileUri, "application/vnd.android.package-archive");
install.setDataAndType(Uri.parse("file://" + fileName.toString()), "application/vnd.android.package-archive");
install.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(install);
这样通过查询本地真实的下载好apk的真实本地路径,然后再进行安装的话就不会报异常了。
参考文章:
http://blog.csdn.net/q445697127/article/details/40537945
http://www.xuebuyuan.com/2163407.html
http://www.2cto.com/kf/201502/376975.html
作者:欧阳鹏 欢迎转载,与人分享是进步的源泉!
转载请保留原文地址:http://blog.csdn.net/ouyang_peng/article/details/53404379
【我的Android进阶之旅】 解决bug: Expected file scheme in URI: content://downloads/my_downloads/12的更多相关文章
- 我的Android进阶之旅------>解决Bug:打开eclipse报错,发现了以元素 'd:skin' 开头的无效内容。此处不应含有子元素。
今天来打开Eclipse 报错了,错误信息如下: [2015-08-01 09:07:43 - Android SDK] Error when loading the SDK: Error: Erro ...
- 我的Android进阶之旅------>解决Jackson等第三方转换Json的开发包在开启混淆后转换的实体类数据都是null的bug
1.错误描述 今天测试人员提了一个bug,说使用我们的app出现了闪退的bug,后来通过debug断点调试,发现我们的app转换服务器发送过来的json数据后,都是为null.而之前已经提测快一个月的 ...
- 我的Android进阶之旅------>解决Error: specified for property 'mergedManifest' does not exist.
错误描述 刚运行从Github上面下载而来的代码时,爆了如下所示的bug,错误描述如下所示: Error:A problem was found with the configuration of t ...
- 我的Android进阶之旅------>解决Android Studio报错:DefaultAndroidProject : Unsupported major.minor version 52.0
问题描述 今天使用Android Studio 2.0打开我之前的项目时,编译报了如下错误: Error:Cause: com/android/build/gradle/internal/model/ ...
- 我的Android进阶之旅------>解决 Error: ShouldNotReachHere() 问题
在Android项目中创建一个包含main()方法的类,直接右键运行该类时会报如下错误: # # An unexpected error has been detected by Java Runti ...
- 我的Android进阶之旅------>解决DownloadManager报错java.lang.SecurityException: Invalid value for visibility: 2
1.问题描述 今天使用Android系统的DownloadManager进行下载操作时,爆了如下所示的错误: java.lang.RuntimeException: Unable to start s ...
- 我的Android进阶之旅------>解决Error:Unable to find method 'org.gradle.api.internal.project.ProjectInternal.g
错误描述 今天在Github上面下载了一份代码,然后导入到Android Studio中直接报了如下图所示的错误: 错误描述如下: Error: Unable to find method 'org. ...
- 我的Android进阶之旅------>解决Error:Could not find property 'compile' on org.gradle.api.internal.artifacts.
1错误描述 解决方法 1错误原因 2解决方法 1.错误描述 刚刚,Android Studio突然编译不了了,报了如下错误: Error:Could not find property 'compil ...
- 我的Android进阶之旅------>解决Jackson、Gson解析Json数据时,Json数据中的Key为Java关键字时解析为null的问题
1.问题描述 首先,需要解析的Json数据类似于下面的格式,但是包含了Java关键字abstract: { ret: 0, msg: "normal return.", news: ...
随机推荐
- day5:协成函数与import、for...import...的使用
一.协程函数 1.把函数的执行结果封装好__iter__和__next__,即得到一个迭代器2.与return功能类似,都可以返回值,但不同的是,return只能返回一次值,而yield可以返回多次值 ...
- python-sqlite3之占位符
The sqlite3 module supports two kinds of placeholders: question marks (qmark style) and named placeh ...
- C++忽略字符大小写比较
在项目中用到对两个字符串进行忽略大小写的比较,有两个方法实现 1.使用C++提供的忽略大小写比较函数实现 代码实现: /* 功能 :忽略大小写进行字符串比较 */ #ifdef __LINUX__ # ...
- NetBeans 设置code completion/auto pop-up delay
如果你在Tools>Options>Editor>Code Completion>Language: Java 没有找到设置delay的选项.那就去C盘(如果你用的是Windo ...
- 重启tomcat但是session仍然有效的解决方法
参考:http://www.blogjava.net/freeman1984/archive/2010/03/30/316901.html server.xml,在你的webapp的Context节点 ...
- webservice统一认证
service package cn.edu.hbcf.privilege.ws; import javax.jws.WebParam; import javax.jws.WebService; @W ...
- Hive学习笔记——保存select结果,Join,多重插入
1. 保存select查询结果的几种方式: 1.将查询结果保存到一张新的hive表中 create table t_tmp as select * from t_p; 2.将查询结果保存到一张已经存在 ...
- 严重: Error configuring application listener of class org.springframework.web.context.ContextLoaderList
最近在用idea部署war文件的时候,总是出现了部署失败的错误,刚开始并没有在意,但是现在次数越来越多了,不得不在意了,然后就在百度上搜,然后就有了各种说法 1,错误的信息是: One or more ...
- 编写Nginx启停服务脚本
在/etc/init.d/目录下创建脚本 vim /etc/init.d/nginx 编写脚本内容:(其中下面2行需要根据情况自行修改) nginxd=/opt/nginx/sbin/nginx ng ...
- Meter and pixel units in a box2d game - LibGDX
http://pimentoso.blogspot.com/2013/01/meter-and-pixel-units-in-box2d-game.html 需FQ ————————————————— ...