Android7.0自动更新适配包解析异常

  

在Android7.0的手机上,自动更新的时候出现包解析异常,在其他的手机上没有这个问题。

  

<强>原因:

  

Android7.0引入私有目录被限制访问和StrictMode API。私有目录被限制访问是指在Android7.0中为了提高应用的安全性,在7.0上应用私有目录将被限制访问.StrictMode API是指禁止向你的应用外公开文件://URI。如果一项包含文件文件://URI类型的意图离开你的应用,则会报出异常。

  

<>强解决办法:

  

在AndroidManifest.xml中注册供应商,供应商可以向应用外提供数据。

        & lt;提供者   android:当局="包名.fileprovider”   android: name=" android.support.v4.content.FileProvider "   android: grantUriPermissions=" true "//这是设置uri的权限   android:出口=" false "比;   & lt;元数据   android: name=" android.support.FILE_PROVIDER_PATHS "   android:资源=癅xml/file_paths”/在//在第二步的时候会有介绍   & lt;/provider>      

在res/xml中创建file_paths.xml文件。

        & lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比;   & lt; resources>   & lt; paths>   & lt; external-path路径=" name="下载"/比;   & lt;/paths>   & lt;/resources>      

贴出我的自动更新下载的代码

        公开课UpdateManager {   私人上下文mContext;      私有静态字符串savePath;   私人字符串saveFileName;   私人ProgressBar mProgress;//下载进度条控件   私有静态最终int下载=1;//表示正在下载   私有静态最终int下载=2;//下载完毕   私有静态最终int DOWNLOAD_FAILED=3;//下载失败   私人int进展;//下载进度   私人布尔cancelFlag=false;//取消下载标志位      私人字符串serverVersion;//从服务器获取的版本号   私人字符串apkUrl;//私人字符串apkUrl=" http://liuliu.lejuhuyu.com/AndroidApk/liuliu-dashou-app-1.0.2.apk ";   私人字符串clientVersion;//客户端当前的版本号   私人字符串updateDescription="请更新当前最新版本”;//更新内容描述信息   私人字符串forceUpdate;//是否强制更新   私人字符串更新;   私人VersionBean mVersionBean;      私人AlertDialog alertDialog1 alertDialog2;//表示提示对话框,进度条对话框   公共UpdateManager(上下文语境,VersionBean VersionBean) {   这一点。mContext=上下文;   这一点。mVersionBean=versionBean;   apkUrl=" http://liuliu.lejuhuyu.com/AndroidApk/liuliu-dashou-app- " + versionBean.getLastVersion () +“. apk”;   savePath=Environment.DIRECTORY_DOWNLOADS;   saveFileName=savePath +/liuliu-dashou-app -“+ versionBean.getLastVersion () +“. apk”;   }/* *显示更新对话框*/公共空间showNoticeDialog () {   serverVersion=mVersionBean.getLastVersion ();   clientVersion=mVersionBean.getVersion ();   于(“apkUrl=" + apkUrl);   于(“savePath=" + savePath);   于(“saveFileName=" + saveFileName);//forceUpdate=StringUtils.getVersion ();//forceUpdate=?”;   forceUpdate=mVersionBean.getImportant ();   更新=mVersionBean.getUpdate ();//如果版本最新,则不需要更新   如果(serverVersion.equals (clientVersion))   返回;   如果(update.equals (“2”)   返回;   AlertDialog。生成器对话框=new AlertDialog.Builder (mContext);   对话框。setTitle(“发现新版本:“+ serverVersion);   dialog.setMessage (updateDescription);   对话框。setPositiveButton(“现在更新”,新DialogInterface.OnClickListener () {   @Override   公共空间onClick (int __arg1 DialogInterface arg0) {//TODO自动生成方法存根   arg0.dismiss ();   showDownloadDialog ();   }   });//是否强制更新   如果(forceUpdate.equals (“2”)) {   对话框。setNegativeButton(“待会更新”,新DialogInterface.OnClickListener () {   @Override   公共空间onClick (int __arg1 DialogInterface arg0) {//TODO自动生成方法存根   arg0.dismiss ();   }   });   }   alertDialog1=dialog.create ();   alertDialog1.setCancelable(假);   alertDialog1.show ();   }/* *显示进度条对话框*/公共空间showDownloadDialog () {   AlertDialog。生成器对话框=new AlertDialog.Builder (mContext);   dialog.setTitle(“正在更新”);   最后LayoutInflater增压泵=LayoutInflater.from (mContext);   视图v=inflater.inflate(出来。softupdate_progress, null);   mProgress=(ProgressBar) v.findViewById (R.id.update_progress);   dialog.setView (v);//如果是强制更新,则不显示取消按钮//如果(forceUpdate.equals (" 1 ")) {//对话框。setNegativeButton(“取消”,新DialogInterface.OnClickListener () {//@ 覆盖//公共空onClick (int __arg1 DialogInterface arg0) {////TODO自动生成方法存根//arg0.dismiss ();//cancelFlag=false;//}//});//}   alertDialog2=dialog.create ();   alertDialog2.setCancelable(假);   alertDialog2.show ();//下丹共载   downloadAPK ();   }   DownloadManager经理;   鼠标光标;   DownloadManager。请求;   DownloadManager。查询查询;   ContentObserver ContentObserver;/* *下载apk的线程*/公共空间downloadAPK () {   经理=(DownloadManager) LiuLiuApplication.getContext () .getSystemService (Context.DOWNLOAD_SERVICE);=new DownloadManager.Request (Uri.parse (apkUrl));//设置允许使用的网络类型,这里是移动网络和无线网络都可以   down.setAllowedNetworkTypes (DownloadManager.Request.NETWORK_MOBILE   | DownloadManager.Request.NETWORK_WIFI);//显示下载界面   down.setVisibleInDownloadsUi(真正的);//设置下载路径和文件名   down.setDestinationInExternalPublicDir(环境。DIRECTORY_DOWNLOADS,“liuliu-dashou-app——“+ mVersionBean.getLastVersion () + " . apk ");   down.setMimeType(“应用程序/vnd.android.package-archive”);//设置为可被媒体扫描器找到   down.allowScanningByMediaScanner ();   down.setAllowedOverRoaming(假);//down.setNotificationVisibility (DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);   长id=manager.enqueue(下);   查询=new DownloadManager.Query () .setFilterById (id);   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

Android7.0自动更新适配包解析异常