Android6.0
The 6
然后在代码里的:
Uri uri = Uri.fromFile(new File(picPath));
更改为:
Uri uri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
uri = FileProvider.getUriForFile(context, "com.luxiaofeng-你的应用包名 和mainfest声明的一致",newFile(picPath));
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
}else{
uri = Uri.fromFile(new File(picPath));
}
intent.setDataAndType (uri, type);
context.startActivity(intent);
但是这里有一个隐形的BUG/也就是阿里巴巴安卓开发规范提出的:
所以写代码得要考虑使用环境 设备的不同 版本的不同等
if (null != type && !TextUtils.isEmpty(type)) {
intent.setDataAndType(uri, type);
if (context.getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY) != null) {
context.startActivity(intent);
} else {
//没有匹配到相关组件
}
}
Android 8.0
The 8
安装apk的权限也是需要动态申请,
Manifest:
Code:
int permission = ActivityCompat.checkSelfPermission(activity,
Manifest.permission.REQUEST_INSTALL_PACKAGES);
if (permission != PackageManager.PERMISSION_GRANTED) {
// We don't have permission so prompt the user
ActivityCompat.requestPermissions(activity, PERMISSONS,
REQUEST_EXTERNAL_STORAGE);
}
private static String[] PERMISSONS= {
Manifest.permission.REQUEST_INSTALL_PACKAGES
};
作者:陆小凤1995