1 --Android 7.0,8.0手机相机权限没有直接崩溃,*切记动态检查权限
public void request(){
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.CAMERA}, 1);
return;
}
}
--权限申请结果查看
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults){
switch (requestCode){
case 1:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
getPicFromCamera();
}else{
Toast.makeText(this, "没有相机权限!\n请接收权限申请或前往设置添加权限!", Toast.LENGTH_SHORT).show();
}
break;
default:
}
}
**** 7.0,8.0 打开相机
---1清单文件配置provide 与activity同级
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path name="rc_external_path" path="."/>
</paths>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.zjxnkj.countrysidecommunity.FileProvider"
android:exported="false"
android:grantUriPermissions="true"
tools:replace="android:authorities">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/rc_file_path"
tools:replace="android:resource" />
</provider>
--2 代码区别
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.ENGLISH).format(new Date());
String imageFileName = "JPEG_" + timeStamp + ".jpg";
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
if (!storageDir.exists()) {
if (!storageDir.mkdir()) {
Log.e("TAG", "Throwing Errors....");
throw new IOException();
}
}
File image = new File(storageDir, imageFileName);
return image;
}
/**
* 从相机获取图片
*/
private void getPicFromCamera() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.CAMERA}, 1);
return;
}
//跳转到调用系统相机
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//用于保存调用相机拍照后所生成的文件// new File(Environment.getExternalStorageDirectory().getPath(), System.currentTimeMillis() + ".jpg");
try {
tempFile =createImageFile();
} catch (IOException e) {
e.printStackTrace();
}
// Ensure that there's a camera activity to handle the intent
if (intent.resolveActivity(getPackageManager()) != null) {
Uri photoFile;
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
String authority = "com.zjxnkj.countrysidecommunity.FileProvider";
photoFile = FileProvider.getUriForFile(getApplicationContext(), authority, tempFile);
} else {
photoFile = Uri.fromFile(tempFile);
}
// Continue only if the File was successfully created
if (photoFile != null) {
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoFile);
}
}
PackageManager packageManager = getPackageManager();
if (packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
startActivityForResult(intent, CAMERA_REQUEST_CODE);
}else {
ToastUtils.showToast(getApplicationContext(),"请打开相机权限");
}
}
2 Android8.0需要申请安装应用权限 读取sd卡和安装应用
public void request(){
if (Build.VERSION.SDK_INT >= 26) {
boolean b = getPackageManager().canRequestPackageInstalls();
if (b) {
String[] perms = {android.Manifest.permission.WRITE_EXTERNAL_STORAGE, android
.Manifest.permission.READ_EXTERNAL_STORAGE};
// String[] perms = {android.Manifest.permission.CAMERA, android.Manifest.permission.ACCESS_FINE_LOCATION};
requestPermissions(perms,"请求访问SD卡");
// mMainPresenter.installApk();
} else {
//请求安装未知应用来源的权限
String[] perms = {android.Manifest.permission.WRITE_EXTERNAL_STORAGE, android
.Manifest.permission.READ_EXTERNAL_STORAGE,android.Manifest.permission.REQUEST_INSTALL_PACKAGES};
// String[] perms = {android.Manifest.permission.CAMERA, android.Manifest.permission.ACCESS_FINE_LOCATION};
requestPermissions(perms,"请求访问SD卡,安装应用");
}
} else {
String[] perms = {android.Manifest.permission.WRITE_EXTERNAL_STORAGE, android
.Manifest.permission.READ_EXTERNAL_STORAGE};
// String[] perms = {android.Manifest.permission.CAMERA, android.Manifest.permission.ACCESS_FINE_LOCATION};
requestPermissions(perms,"请求访问SD卡");
}
}
---结果重写 onPermissionsGranted
@Override
public void onPermissionsGranted(int requestCode, List<String> perms) {
updateManager.installApk();
}
@Override
public void onPermissionsDenied(int requestCode, List<String> perms) {
if (perms.size() > 0 && Manifest.permission.REQUEST_INSTALL_PACKAGES.equals(perms.get(0))) {
// 引导用户手动开启安装权限
ToastUtils.showToast(getApplicationContext(), "申请安装应用权限失败,\n请选择村知书打开安装权限", Toast.LENGTH_LONG);
Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES);
startActivityForResult(intent, GET_UNKNOWN_APP_SOURCES);
} else {
requestPermissions();
}
}
3. Android11安装apk(授予权限系统也会杀死)
Android 11上发生了变化,安装APK的应用会被强制关闭。(授予权限也会强制关闭应用)
当你再次打开之前的应用时,会发现它已经完全被重新加载,可能会出现丢失输入或其他尚未缓存的数据,因为应用已经被系统杀死了。Android通常只有在用户撤销权限时才会强行退出应用,以阻止应用被破解,而不是在用户授予权限时强行退出。
IT之家了解到,谷歌似乎对此相当淡定,最初只表示“这实际上是在按原定计划工作”。但开发者表示这可能会破坏那些不希望在操作过程中被强行停止的应用,谷歌才提供了进一步的评论,明确表示这一变化与新的Scoped Storage强制分区存储文件管理有关。虽然Scoped Storage将有利于安全和隐私,但新系统显然会带来比预期的更大的变化。