场景

安卓手机在进行图片上传时如果手机没有授权”存储”权限会出现上传失败提示。
参考链接
https://ask.dcloud.net.cn/article/35091
https://www.html5plus.org/doc/zh_cn/android.html#plus.android.requestPermissions
/*正常权限,无需动态申请:ACCESS_LOCATION_EXTRA_COMMANDSACCESS_NETWORK_STATEACCESS_NOTIFICATION_POLICYACCESS_WIFI_STATEBLUETOOTHBLUETOOTH_ADMINBROADCAST_STICKYCHANGE_NETWORK_STATECHANGE_WIFI_MULTICAST_STATECHANGE_WIFI_STATEDISABLE_KEYGUARDEXPAND_STATUS_BARGET_PACKAGE_SIZEINSTALL_SHORTCUTINTERNETKILL_BACKGROUND_PROCESSESMODIFY_AUDIO_SETTINGSNFCREAD_SYNC_SETTINGSREAD_SYNC_STATSRECEIVE_BOOT_COMPLETEDREORDER_TASKSREQUEST_INSTALL_PACKAGESSET_ALARMSET_TIME_ZONESET_WALLPAPERSET_WALLPAPER_HINTSTRANSMIT_IRUNINSTALL_SHORTCUTUSE_FINGERPRINTVIBRATEWAKE_LOCKWRITE_SYNC_SETTINGS2)危险权限,需要动态申请:group:android.permission-group.STORAGEREAD_EXTERNAL_STORAGEWRITE_EXTERNAL_STORAGEgroup:android.permission-group.CONTACTSWRITE_CONTACTSGET_ACCOUNTSREAD_CONTACTSgroup:android.permission-group.PHONEREAD_CALL_LOGREAD_PHONE_STATECALL_PHONEWRITE_CALL_LOGUSE_SIPPROCESS_OUTGOING_CALLScom.android.voicemail.permission.ADD_VOICEMAILgroup:android.permission-group.CALENDARREAD_CALENDARWRITE_CALENDARgroup:android.permission-group.CAMERACAMERAgroup:android.permissiongroup.SENSORSBODY_SENSORSgroup:android.permission-group.LOCATIONACCESS_FINE_LOCATIONACCESS_COARSE_LOCATIONgroup:android.permission-group.MICROPHONERECORD_AUDIOgroup:android.permission-group.SMSREAD_SMSRECEIVE_WAP_PUSHRECEIVE_MMSRECEIVE_SMSSEND_SMSREAD_CELL_BROADCASTS*//*** Me:* cnscn <214363570@qq.com>** 参考:* https://blog.csdn.net/lvyoujt/article/details/52826556* https://developer.android.com/reference/android/Manifest.permission* https://developer.android.com/reference/android/os/Build.VERSION*/// 进行平台条件编译,否则在iOS端会出现实例对象找不到的报错情况const platform = uni.getSystemInfoSync().platform == 'android'const Build = platform ? plus.android.importClass('android.os.Build'):''const Manifest = platform ? plus.android.importClass('android.Manifest'):""const MainActivity = platform ? plus.android.runtimeMainActivity():""export const ArrPermissions = platform ? [Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.CAMERA,] : []export function PermissionCheck(permission) {if (Build.VERSION.SDK_INT >= 23) {if (MainActivity.checkSelfPermission(permission) == -1) {return false}}return true}export function PermissionChecks(Arr) {var HasPermission = truefor (var index in Arr) {var permission = Arr[index]//如果此处没有权限,则是用户拒绝了if (!PermissionCheck(permission)) {HasPermission = falsebreak}}return HasPermission}// 实测 ainActivity.requestPermissions(Arr, REQUEST_CODE_CONTACT)申请权限无法调起// 使用plus.andriod 方法进行替换export function PermissionRequest(Arr) {var REQUEST_CODE_CONTACT = 101if (Build.VERSION.SDK_INT >= 23) {// MainActivity.requestPermissions(Arr, REQUEST_CODE_CONTACT)plus.android.requestPermissions(Arr, function (e) {}, function (e) {});}}//如果没有权限,则申请// if (!PermissionChecks(ArrPermissions)) {// PermissionRequest(ArrPermissions);// } else { //如果拥有权限,那么干点啥吧^_^// //.......// }
进行权限申请以及图片上传
/* #ifdef MP-WEIXIN */this.chooseImage();/* #endif *//* #ifdef APP-PLUS || H5*/if (window.plus && plus.os.name == "Android") {if (!PermissionChecks(ArrPermissions)) {PermissionRequest(ArrPermissions);} else {this.chooseImage();}} else {this.chooseImage();}/* #endif */
