ionic混合开发调用Android系统相册,官方的cordova-image-picker插件在直接用的时候会在Android6.0以上的版本由于没有获得相应的存储权限而闪退。
解决方法一:
在原有的cordova插件上面改动,添加了动态授权,我放在了github 用法和原来的相同。
function getImgPicker() { var options = { maximumImagesCount: 1, width: 800, height: 800, quality: 80, title:'', message:'', outputType:1 }; window.imagePicker.getPictures(function (results) { vm.userInfo.Url = results[0]; console.log(vm.userInfo.Url); vm.upload(vm.userInfo.Url); }, function (err) { publicMethod.showLoadingNoBackdrop('调取相册失败'); console.log(angular.toJson(error, true)); }, options); }
解决方法二:
添加一个新的插件,专门获取所需的权限cordova-plugin-android-permissions,下面是用法。
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); } }
ionic 监听Android的返回键和双击退出程序(直接上代码)
$ionicPlatform.registerBackButtonAction(function (e) { e.preventDefault(); //判断处于哪个页面时双击退出 if ($location.path() == '/tab/home' || $location.path() == '/tab/activity' || $location.path() == '/tab/person' || $location.path() == '/tab/share' || $location.path() == '/tab/study') { if ($rootScope.backButtonPressedOnceToExit) { ionic.Platform.exitApp(); } else { $rootScope.backButtonPressedOnceToExit = true; $cordovaToast.show("再按一次退出系统", "short", "bottom"); $timeout(function () { $rootScope.backButtonPressedOnceToExit = false; }, 2000); } } else if ($location.path() == '/blueprint-list' || $location.path() == '/megagame-list') { $state.go('tab.activity'); //此处判断当前所处的View,并返回到tab页 } else if ($ionicHistory.backView()) { $ionicHistory.goBack(); } else { if ($rootScope.backButtonPressedOnceToExit) { ionic.Platform.exitApp(); } else { $rootScope.backButtonPressedOnceToExit = true; $cordovaToast.show("再按一次退出系统", "short", "bottom"); $timeout(function () { $rootScope.backButtonPressedOnceToExit = false; }, 2000); } } return false; }, 101);
作者:Nickyzhang
链接:https://www.jianshu.com/p/4cfa6652fb3f