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,调用,相机
0
投稿
猜你喜欢
解决@Autowired注入static接口的问题
2022-01-02 03:16:29
Android实现列表数据按名称排序、中英文混合排序
2022-10-18 15:15:57
Android 7.0系统webview 显示https页面空白处理方法
2021-10-22 09:36:49
Android中GIF动图的播放控制和监听详解
2023-12-08 18:15:01
MyBatis超详细讲解如何实现分页功能
2023-08-22 23:06:51
Scala方法与函数使用和定义详解
2021-10-05 06:34:12
基于Java信号量解决死锁过程解析
2023-05-13 22:23:02
浅析Bean Searcher 与 MyBatis Plus 区别介绍
2022-08-28 03:25:41
浅谈java中String的两种赋值方式的区别
2021-09-21 23:59:36
C#使用is、as关键字以及显式强转实现引用类型转换
2023-01-29 16:15:27
Java的类型擦除式泛型详解
2022-01-02 11:59:00
C#使用表达式树(LambdaExpression)动态更新类的属性值(示例代码)
2022-05-17 11:46:16
java实现简单的猜数字小游戏
2023-11-11 12:55:40
一场由Java中Integer引发的踩坑实战
2021-09-06 11:14:40
c#创建圆形类Circle、矩形类实现代码
2023-08-18 19:59:12
Java实现显示指定类型的文件
2021-10-26 11:30:37
C#中进程的挂起与恢复
2022-05-17 13:33:26
SpringMVC+EasyUI实现页面左侧导航菜单功能
2022-09-30 17:11:01
带你走进Maven的大门-最全Maven配置及集成idea工具总结
2022-12-06 08:41:40
浅谈Maven的安装及修改为阿里云下载依赖
2023-08-05 08:30:37