笔记:requires the provider be exported, or grantUriPermission()
来源:互联网 发布:windows已阻止此软件 编辑:程序博客网 时间:2024/05/22 01:39
背景:
之前更新是通过Notification中加入PendingIntent,正常运行,现在改为下载apk后在当前Activity中启动安装。
代码:
原先使用Notification的代码如下
Intent intent = new Intent(Intent.ACTION_VIEW);intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);Uri uri;if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { uri = FileProvider.getUriForFile(mContext, BuildConfig.APPLICATION_ID + ".provider", saveFilePath);} else { uri = Uri.fromFile(saveFilePath);}intent.setDataAndType(uri, "application/vnd.android.package-archive");PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);Notification notification = new Notification.Builder(mContext).setAutoCancel(true).setOngoing(true).setContentTitle(mContext.getString(R.string.downloadSuccess)).setContentIntent(pendingIntent).setSmallIcon(R.mipmap.appicon).setWhen(System.currentTimeMillis()).setTicker(mContext.getString(R.string.downloadSuccess)).build(); mNotificationManager.notify(R.mipmap.appicon, notification);
修改之后的代码如下(和上面代码不在同一个类中)
Intent intent= new Intent(Intent.ACTION_VIEW); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);Uri uri;if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { uri = FileProvider.getUriForFile(mBaseContext,BuildConfig.APPLICATION_ID + ".provider", apkPath); intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);} else { uri = Uri.fromFile(apkPath);}intent.setDataAndType(uri, "application/vnd.android.package-archive");android.os.Process.killProcess(android.os.Process.myPid());
报错:
java.lang.SecurityException: Permission Denial
requires the provider be exported, or grantUriPermission()
原因:
没有获取到FLAG_GRANT_READ_URI_PERMISSION权限,需要调用grantUriPermission()方法获取,参考How to use support FileProvider for sharing content to other apps?
改正后的代码:
...uri = FileProvider.getUriForFile(mBaseContext,BuildConfig.APPLICATION_ID + ".provider", apkPath);grantUriPermission(getPackageName(),uri,Intent.FLAG_GRANT_READ_URI_PERMISSION); intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); ...
阅读全文
0 0
- 笔记:requires the provider be exported, or grantUriPermission()
- requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
- requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
- Android: requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
- Android: requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
- Why the “MAVEN2_CLASSPATH_CONTAINER will not be exported or published”
- androidbug之Error running CaptureActivity: The activity must be exported or contain an intent-filter
- BUG:android The activity must be exported or contain an intent-filter
- The activity must be exported or contain an intent-filter解决方法
- android studio 运行出现“The activity must be exported or contain an intent-filter”
- Android Studio 3 Error Message:The activity must be exported or contain an intent-filter
- will not be exported or published. Runtime ClassNotFoundExceptions may result
- Android N requires the IDE to be running with Java 1.8 or later
- Android N requires the IDE to be running with Java 1.8 or later
- requires the IDE to be running with java 1.8 or later 解决方法
- 解决 Android N requires the IDE to be running with Java 1.8 or later
- Android N requires the IDE to be running with Java 1.8 or later 问题解决
- Android N requires the IDE to be running with Java 1.8 or later 解决方法
- (源码阅读)Resources资源加载流程
- 事件驱动,线程池,非阻塞,异常处理(node大神的有时间常看)
- 页面元素坐标和偏移(clientX/pageX/screenX/layerX/offsetWidth/scrollWidth/clientWidth等)相关整理
- yii 表单小物件使用注意事项
- (四)gradle构建spring-boot项目
- 笔记:requires the provider be exported, or grantUriPermission()
- Java中BigDecimal的使用
- [BZOJ]3438: 小M的作物 最小割
- c++中的多线程
- github (mac)记录
- 在cygwin上面安装gcc
- Flask 学习笔记 --12
- 数据冒险之顺序表应用
- jQuery选择器——子元素过滤选择器