毋庸置疑,以前targetSdkVersion是22就是懒得处理权限导致的,应了一句话,欠下的总要还的.
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
startActivityForResult(intent, PHOTO_REQUEST_TAKEPHOTO);
/*
* 如果是6.0以上才去判断是否需要判断运行时权限,6.0以下不考虑
*/
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.CAMERA}, CAMERA_PERMISSION);
return;
}
}else{
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case CAMERA_PERMISSION:
if (permissions[0].equals(Manifest.permission.CAMERA)) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//申请成功之后,跳转拍照界面
}
}
break;
default:
break;
}
}
所以如果你还是使用刚才那个打开相机的代码,你会发现,就算有了权限,照样crash.所以你还需要以下操作.
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(getPackageManager()) != null) {
/*
* 指定拍照存储路径
* 7.0 及其以上使用FileProvider替换'file://'访问
*/
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//这里的BuildConfig,需要是程序包下BuildConfig。
intent.putExtra(MediaStore.EXTRA_OUTPUT, FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".provider", photoFile));
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
} else {
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
}
startActivityForResult(intent, PHOTO_REQUEST_TAKEPHOTO);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
int NOTIFICATION_ID = (int) (System.currentTimeMillis()%10000);
NotificationChannel channel = new NotificationChannel("hh","name", NotificationManager.IMPORTANCE_HIGH);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.createNotificationChannel(channel);
Notification notification = new Notification.Builder(getApplicationContext(),"hh").build();
startForeground(NOTIFICATION_ID,notification);
}
你会发现在8.0上发送广播接收不到了,尴尬不,意外不.<<
Android8.0 静态receiver接收不到隐式广播>>
intent.setPackage(getPackageName());
持续更新中…也许还有很多问题需要去发现和解决…
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- niushuan.com 版权所有 赣ICP备2024042780号-2
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务