【我的Android进阶之旅】 解决bug: Expected file scheme in URI: content://downloads/my_downloads/12

简介: 一、错误描述今天测试MM用HTC手机测试某个模块的时候crash了,抓log后发现是使用DownloadManager下载apk安装包然后自动安装的时候,抛了异常:java.

一、错误描述

今天测试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

这里写图片描述

相关文章
|
5月前
|
XML Java Android开发
Android Studio开发之使用内容组件Content获取通讯信息讲解及实战(附源码 包括添加手机联系人和发短信)
Android Studio开发之使用内容组件Content获取通讯信息讲解及实战(附源码 包括添加手机联系人和发短信)
102 0
|
3月前
|
Android开发
【Bug】Android resource linking failed和error: failed linking references.
【Bug】Android resource linking failed和error: failed linking references.
|
1天前
|
API Android开发
Android高手进阶教程(十五)之---通过Location获取Address的使用!
Android高手进阶教程(十五)之---通过Location获取Address的使用!
|
1月前
|
XML Android开发 数据格式
Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class
Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class
23 0
|
1月前
|
Android开发
Android Uri转File方法(适配android 10以上版本及android 10以下版本)
Android Uri转File方法(适配android 10以上版本及android 10以下版本)
31 0
|
2月前
|
XML 数据格式
android.view.InflateException: Binary XML file line #0: Attempt to invoke virtual
android.view.InflateException: Binary XML file line #0: Attempt to invoke virtual
10 0
|
7月前
Android-async-http 添加token get方法报错 No valid URI scheme was provided
Android-async-http 添加token get方法报错 No valid URI scheme was provided
|
8月前
|
编解码 网络协议 Android开发
Android平台RTMP|RTSP直播播放器功能进阶探讨
很多开发者在跟我聊天的时候,经常问我,为什么一个RTMP或RTSP播放器,你们需要设计那么多的接口,真的有必要吗?带着这样的疑惑,我们今天聊聊Android平台RTMP、RTSP播放器常规功能,如软硬解码设置、实时音量调节、实时快照、实时录像、视频view翻转和旋转、画面填充模式设定、解码后YUV、RGB数据回调等:
111 0
|
9月前
|
XML Android开发 数据格式
Android Binary XML file line #50: Error inflating class androidx.cardview.widget.CardView 错误
Android Binary XML file line #50: Error inflating class androidx.cardview.widget.CardView 错误
52 0
|
12月前
|
Android开发
Bug日志(四)——Android 防止多次点击(另解决多个列表子项点击)
在用户使用 应用的时候,经常会出现点击过快且多次点击同一控件的情况, 一方面这是因为应用或手机当前有些卡顿,网络卡啥的 另一方面也可能是由于很多应用并没有设置按钮点击时的 selector 或者其它按钮响应方式(例如点击按钮时按钮放大,常见于游戏),导致用户误认为没有点击到当前按钮。(比如使用selector再点击后更换背景颜色,图片等等)