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

来源:互联网 发布:恒生电子 软件下载 编辑:程序博客网 时间:2024/06/04 19:31
关于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>


  • files-path/>代表的根目录: Context.getFilesDir()

  • <external-path/>代表的根目录: Environment.getExternalStorageDirectory()

  • <cache-path/>代表的根目录: getCacheDir()


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

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







    阅读全文
    0 0
    原创粉丝点击