Android 6.0调用相机图册崩溃的完美解决方案

Android 6.0调用相机图册崩溃的完美解决方案,第1张

概述最近客户更新系统发现,以前的项目在调用相机的时候,闪退掉了,很奇怪,后来查阅后发现,Android6.0以后需要程序授权相机权限,默认会给出提示,让用户授权,个人感觉这一特性很好,大概如下:

最近客户更新系统发现,以前的项目在调用相机的时候,闪退掉了,很奇怪,后来查阅后发现,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() {@OverrIDepublic 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,REQUEST_CAMERA);}// END_INCLUDE(camera_permission_request)

授权后接收回调方法:

/*** Callback received when a permissions request has been completed.*/@OverrIDepublic 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_LOCATIONpermissions.CAMERApermissions.GET_ACCOUNTSpermissions.READ_CONTACTSpermissions.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调用相机图册崩溃的完美解决方案,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!

总结

以上是内存溢出为你收集整理的Android 6.0调用相机图册崩溃的完美解决方案全部内容,希望文章能够帮你解决Android 6.0调用相机图册崩溃的完美解决方案所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1148101.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存