我的Android进阶之旅------>解决DownloadManager报错java.lang.SecurityException: Invalid value for visibility: 2

1、问题描述

今天使用Android系统的DownloadManager进行下载操作时,爆了如下所示的错误:

java.lang.RuntimeException: Unable to start service com.xtc.fm.qingting.services.QingtingFmDownloadService2@1b97b060 with Intent { act=com.xtc.fm.qingting.FM_DOWNLOAD_SERVICE_START pkg=com.xtc.fm.qingting cmp=com.xtc.fm.qingting/.services.QingtingFmDownloadService2 (has extras) }: java.lang.SecurityException: Invalid value for visibility: 2
    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2911)
    at android.app.ActivityThread.access$2100(ActivityThread.java:151)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1401)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5254)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:955)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:750)
Caused by: java.lang.SecurityException: Invalid value for visibility: 2
    at android.os.Parcel.readException(Parcel.java:1592)
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:190)
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:142)
    at android.content.ContentProviderProxy.insert(ContentProviderNative.java:476)
    at android.content.ContentResolver.insert(ContentResolver.java:1207)
    at android.app.DownloadManager.enqueue(DownloadManager.java:946)
    at com.xtc.fm.qingting.services.QingtingFmDownloadService2.initDownManager(QingtingFmDownloadService2.java:195)
    at com.xtc.fm.qingting.services.QingtingFmDownloadService2.onStartCommand(QingtingFmDownloadService2.java:120)
    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2894)
    ... 9 more
java.lang.SecurityException: Invalid value for visibility: 2
    at android.os.Parcel.readException(Parcel.java:1592)
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:190)
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:142)
    at android.content.ContentProviderProxy.insert(ContentProviderNative.java:476)
    at android.content.ContentResolver.insert(ContentResolver.java:1207)
    at android.app.DownloadManager.enqueue(DownloadManager.java:946)
    at com.xtc.fm.qingting.services.QingtingFmDownloadService2.initDownManager(QingtingFmDownloadService2.java:195)
    at com.xtc.fm.qingting.services.QingtingFmDownloadService2.onStartCommand(QingtingFmDownloadService2.java:120)
    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2894)
    at android.app.ActivityThread.access$2100(ActivityThread.java:151)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1401)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5254)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:955)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:750)

其中相关的部分代码如下:

 //设置下载地址
        DownloadManager.Request downRequest = new DownloadManager.Request(Uri.parse(downloadUrl));
        // 设置允许使用的网络类型 (| DownloadManager.Request.NETWORK_MOBILE)
        downRequest.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
        // 下载时,不显示通知栏
        downRequest.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);
        // 显示下载界面
        downRequest.setVisibleInDownloadsUi(true);
        // 保存的文件名
        String fileName = downloadInfo.getProgramTitle() + ".m4a";
        // 设置下载路径和文件名
        downRequest.setDestinationInExternalPublicDir(Environment.DIRECTORY_MUSIC, fileName);
        //设置文件类型
        MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
        String mimeString = mimeTypeMap.getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(downloadUrl));
        downRequest.setMimeType(mimeString);
        // 设置为可被媒体扫描器找到
        downRequest.allowScanningByMediaScanner();
        // 将下载请求放入队列
        final long downloadId = manager.enqueue(downRequest);

2、解决方法

原来其中的一段代码是需要相应的权限 android.permission.DOWNLOAD_WITHOUT_NOTIFICATION

  // 下载时,不显示通知栏
  downRequest.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);

因此在AndroidManifest.xml文件中加入上面所需要的权限即可。

<!--DownloadManager下载时不显示Notification-->
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />

作者:欧阳鹏 欢迎转载,与人分享是进步的源泉!
转载请保留原文地址:http://blog.csdn.net/ouyang_peng

我的Android进阶之旅------&gt;解决DownloadManager报错java.lang.SecurityException: Invalid value for visibility: 2

上一篇:散列——动机引入


下一篇:《51单片机应用开发从入门到精通》——1.5 测试方法和工具