java.lang.NullPointerException Attempt to invoke virtual method 'android.content.res.XmlResourcePars

关于7.0版本升级包安装时产生的一个错误 百度了下 问题长生的原因大概就是7.0对隐私权限进行了修改 app内如使用的uri不能用intent提供给外部使用 否则会报这个异常
直接用的网上的解决办法

/***
 * 升级服务
 */
public class UpdateService extends Service {

    private static String down_url;
    private BroadcastReceiver receiver;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        try {
            down_url = intent.getStringExtra("Key_Down_Url");
        } catch (Exception e) {
        }
        receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                Log.d("下载完成", "开始安装");
                installApk();
                //销毁当前的Service
                stopSelf();
            }
        };
        registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
        startDownload(down_url);

        return super.onStartCommand(intent, flags, startId);
    }


    public void installApk() {
        /*********下载完成,点击安装***********/
        Intent intent = new Intent();
        intent.setAction(android.content.Intent.ACTION_VIEW);
//        Uri uri;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            File apkFile =
                    new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "test.apk");
//            File imagePath = new File(FileUtil.getUpDatePath());
            Log.d("imagePath", "" + apkFile);
            Uri contentUri = FileProvider.getUriForFile(getApplicationContext(),
                    "包名.provider",
                    apkFile);
            Log.d("下载完成", "getUriForFile" + contentUri);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
//            Log.d("", "" + Uri.parse(new File(FileUtil.getUpDatePath(), "jsonbao.apk").getAbsolutePath()));
        } else {
            Uri downloadFileUri = dm.getUriForDownloadedFile(enqueue);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setDataAndType(downloadFileUri, "application/vnd.android.package-archive");
        }
        Log.d("下载完成", "startActivity");
        startActivity(intent);
        stopSelf();
    }


    /**
     * 系统下载管理器
     */
    private DownloadManager dm;
    /**
     * 系统下载器分配的唯一下载任务id,可以通过这个id查询或者处理下载任务
     */
    private long enqueue;


    private void startDownload(String downUrl) {
        try {
            //获得系统下载器
            dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
            //设置下载地址
            DownloadManager.Request request = new DownloadManager.Request(Uri.parse(downUrl));
            //设置下载文件的类型
            request.setMimeType("application/vnd.android.package-archive");
            //设置下载存放的文件夹和文件名字
            request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "test.apk");
            //设置下载时或者下载完成时,通知栏是否显示
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
            request.setTitle("下载新版本");
            //执行下载,并返回任务唯一id
            enqueue = dm.enqueue(request);
        } catch (Exception e) {
            stopSelf();
        }
    }


    @Override
    public void onDestroy() {
        //服务销毁的时候 反注册广播
        unregisterReceiver(receiver);
        super.onDestroy();
    }


    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

}

在AndroidManifest.xml添加共享文件的设置

<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="包名.provider"
    android:grantUriPermissions="true"
    android:exported="false"
    >
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_paths" />
</provider>


在res 中创建xml文件 明天与resouce里面一致就行

<?xml version="1.0" encoding="utf-8"?>
<paths>

    <external-path
        name="test"
        path="" />

</paths>


  
  

Environment.DIRECTORY_DOWNLOADS
这个是根目录下的download文件夹
之前试验了我自定义的路径但是还是解析失败了  所以就不研究了 直接使用这个方案上的地址 如果有好的解决方案欢迎分享

这样就ok了 测试了2个7.0的手机都没问题







  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值