Android 6.0调用相机图册崩溃的完美解决方案
作者:天外野草 时间:2022-10-13 02:00:39
最近客户更新系统发现,以前的项目在调用相机的时候,闪退掉了,很奇怪,后来查阅后发现,Android 6.0以后需要程序授权相机权限,默认会给出提示,让用户授权,个人感觉这一特性很好,大概如下:
导入Android V4, V7包!
Android Studio 导入很简单,右键项目后找到dependency就ok了。
继承AppCompatActivity
public class MainActivity extends AppCompatActivity
引入需要的类库
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
检查相机权限,并请求权限
// BEGIN_INCLUDE(camera_permission_request)
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.CAMERA)) {
// Provide an additional rationale to the user if the permission was not granted
// and the user would benefit from additional context for the use of the permission.
// For example if the user has previously denied the permission.
Log.i(TAG,
"Displaying camera permission rationale to provide additional context.");
Snackbar.make(mLayout, R.string.permission_camera_rationale,
Snackbar.LENGTH_INDEFINITE)
.setAction(R.string.ok, new View.OnClickListener() {
@Override
public void onClick(View view) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.CAMERA},
REQUEST_CAMERA);
}
})
.show();
} else {
// Camera permission has not been granted yet. Request it directly.
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA},
REQUEST_CAMERA);
}
// END_INCLUDE(camera_permission_request)
授权后接收回调方法:
/**
* Callback received when a permissions request has been completed.
*/
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
if (requestCode == REQUEST_CAMERA) {
// BEGIN_INCLUDE(permission_result)
// Received permission result for camera permission.
Log.i(TAG, "Received response for Camera permission request.");
// Check if the only required permission has been granted
}
}
混合应用开发解决方案
对于混合应用开发,有两种解决方案:
更改现有插件,添加权限访问代码(可能有有些麻烦)
调用权限插件:
安装:
cordova plugin add cordova-plugin-android-permissions@0.10.0
包含的权限
// Example
permissions.ACCESS_COARSE_LOCATION
permissions.CAMERA
permissions.GET_ACCOUNTS
permissions.READ_CONTACTS
permissions.READ_CALENDAR
...
示例代码
var permissions = cordova.plugins.permissions;
permissions.hasPermission(permissions.CAMERA, checkPermissionCallback, null);
function checkPermissionCallback(status) {
if(!status.hasPermission) {
var errorCallback = function() {
console.warn('Camera permission is not turned on');
}
permissions.requestPermission(
permissions.CAMERA,
function(status) {
if(!status.hasPermission) errorCallback();
},
errorCallback);
}
}
以上所述是小编给大家介绍的Android 6.0调用相机图册崩溃的完美解决方案网站的支持!
来源:http://blog.csdn.net/jiangbo_phd/article/details/52613463
标签:android,调用,相机
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SpringBoot多环境开发与日志小结
2021-09-18 19:05:44
![](https://img.aspxhome.com/file/2023/9/78919_0s.png)
详解Android版本适配:9.0 Pie
2022-08-07 05:37:43
![](https://img.aspxhome.com/file/2023/3/108253_0s.jpg)
命令行编译java文件方式
2023-01-18 18:35:47
![](https://img.aspxhome.com/file/2023/2/60252_0s.png)
mybatis-plus QueryWrapper自定义查询条件的实现
2023-04-05 20:50:07
Android轻松画出触摸轨迹
2022-05-07 11:02:03
![](https://img.aspxhome.com/file/2023/7/96757_0s.jpg)
Java实战之实现一个好用的MybatisPlus代码生成器
2023-06-18 02:16:10
![](https://img.aspxhome.com/file/2023/8/57498_0s.png)
mybatis源码解读-Java中executor包的语句处理功能
2023-09-03 06:34:16
关于Feign调用服务Headers传参问题
2022-10-31 01:48:24
![](https://img.aspxhome.com/file/2023/5/71025_0s.jpg)
springboot+thymeleaf 文件上传功能的实现代码
2023-11-25 05:08:59
![](https://img.aspxhome.com/file/2023/3/60053_0s.png)
C#对象为Null模式(Null Object Pattern)实例教程
2023-07-27 15:53:28
![](https://img.aspxhome.com/file/2023/8/89788_0s.png)
新手学习微服务SpringCloud项目架构搭建方法
2022-01-29 04:00:17
浅谈Android Studio如何Debug对应so文件C/C++代码
2023-11-20 03:23:35
![](https://img.aspxhome.com/file/2023/2/89552_0s.jpg)
Java基础知识精通二维数组的应用
2022-02-03 03:01:28
Java(TM) Platform SE binary 打开jar文件的操作
2021-10-02 00:08:12
![](https://img.aspxhome.com/file/2023/4/62534_0s.jpg)
Intellij IDEA根据maven依赖名查找它是哪个pom.xml引入的(图文详解)
2023-07-20 07:49:35
![](https://img.aspxhome.com/file/2023/4/100354_0s.jpg)
解决java执行cmd命令调用ffmpeg报错Concat error - No such filter '[0,0]'问题
2023-03-14 20:35:11
![](https://img.aspxhome.com/file/2023/4/101924_0s.png)
C# 通过反射初探ORM框架的实现原理(详解)
2023-08-30 20:05:30
详解C# FileStream类
2022-10-19 13:44:27
C#实现Datatable排序的方法
2022-12-30 11:31:52
详解SpringBoot Start组件开发之记录接口日志信息
2023-07-26 18:33:34
![](https://img.aspxhome.com/file/2023/5/57835_0s.jpg)