最新安卓SDK 3.94 版本在uni-app中拍照功能无法使用
最新安卓SDK 3.94 版本在uni-app中拍照功能无法使用
产品分类 | uni小程序SDK |
---|---|
手机系统 | Android |
手机系统版本号 | Android 13 |
手机厂商 | 华为 |
手机机型 | 华为畅享20Pro |
页面类型 | vue |
SDK版本号 | 3.94 |
产品分类:uni小程序SDK
手机系统:Android
手机系统版本号:Android 13
手机厂商:华为
手机机型:华为畅享20Pro
页面类型:vue
SDK版本号:3.94
示例代码:
<template>
<view>
<page-head :title="title"></page-head>
<view class="uni-common-mt">
<form>
<view class="uni-list">
<view class="uni-list-cell">
<view class="uni-list-cell-left">
<view class="uni-label">图片来源</view>
</view>
<view class="uni-list-cell-right">
<picker :range="sourceType" [@change](/user/change)="sourceTypeChange" :value="sourceTypeIndex" mode="selector">
<view class="uni-input">{{sourceType[sourceTypeIndex]}}</view>
</picker>
</view>
</view>
<view class="uni-list-cell">
<view class="uni-list-cell-left">
<view class="uni-label">图片质量</view>
</view>
<view class="uni-list-cell-right">
<picker :range="sizeType" [@change](/user/change)="sizeTypeChange" :value="sizeTypeIndex" mode="selector">
<view class="uni-input">{{sizeType[sizeTypeIndex]}}</view>
</picker>
</view>
</view>
<view class="uni-list-cell">
<view class="uni-list-cell-left">
<view class="uni-label">数量限制</view>
</view>
<view class="uni-list-cell-right">
<picker :range="count" [@change](/user/change)="countChange" mode="selector">
<view class="uni-input">{{count[countIndex]}}</view>
</picker>
</view>
</view>
</view>
<view class="uni-list list-pd">
<view class="uni-list-cell cell-pd">
<view class="uni-uploader">
<view class="uni-uploader-head">
<view class="uni-uploader-title">点击可预览选好的图片</view>
<view class="uni-uploader-info">{{imageList.length}}/9</view>
</view>
<view class="uni-uploader-body">
<view class="uni-uploader__files">
<block v-for="(image,index) in imageList" :key="index">
<view class="uni-uploader__file">
<image class="uni-uploader__img" :src="image" :data-src="image" [@tap](/user/tap)="previewImage"></image>
</view>
</block>
<view class="uni-uploader__input-box">
<view class="uni-uploader__input" [@tap](/user/tap)="chooseImage"></view>
</view>
</view>
</view>
</view>
</view>
</form>
</view>
</view>
</template>
<script>
import permision from "@/common/permission.js"
var sourceType = [
['camera'],
['album'],
['camera', 'album']
]
var sizeType = [
['compressed'],
['original'],
['compressed', 'original']
]
export default {
data() {
return {
title: 'choose/previewImage',
imageList: [],
sourceTypeIndex: 2,
sourceType: ['拍照', '相册', '拍照或相册'],
sizeTypeIndex: 2,
sizeType: ['压缩', '原图', '压缩或原图'],
countIndex: 8,
count: [1, 2, 3, 4, 5, 6, 7, 8, 9]
}
},
onUnload() {
this.imageList = [],
this.sourceTypeIndex = 2,
this.sourceType = ['拍照', '相册', '拍照或相册'],
this.sizeTypeIndex = 2,
this.sizeType = ['压缩', '原图', '压缩或原图'],
this.countIndex = 8;
},
methods: {
sourceTypeChange: function(e) {
this.sourceTypeIndex = parseInt(e.detail.value)
},
sizeTypeChange: function(e) {
this.sizeTypeIndex = parseInt(e.detail.value)
},
countChange: function(e) {
this.countIndex = e.detail.value;
},
chooseImage: async function() {
// #ifdef APP-PLUS
// TODO 选择相机或相册时 需要弹出actionsheet,目前无法获得是相机还是相册,在失败回调中处理
if (this.sourceTypeIndex !== 2) {
let status = await this.checkPermission();
if (status !== 1) {
return;
}
}
// #endif
if (this.imageList.length === 9) {
let isContinue = await this.isFullImg();
console.log("是否继续?", isContinue);
if (!isContinue) {
return;
}
}
uni.chooseImage({
sourceType: sourceType[this.sourceTypeIndex],
sizeType: sizeType[this.sizeTypeIndex],
count: this.imageList.length + this.count[this.countIndex] > 9 ? 9 - this.imageList.length : this.count[this.countIndex],
success: (res) => {
this.imageList = this.imageList.concat(res.tempFilePaths);
},
fail: (err) => {
console.log("err: ",err);
// #ifdef APP-PLUS
if (err['code'] && err.code !== 0 && this.sourceTypeIndex === 2) {
this.checkPermission(err.code);
}
// #endif
// #ifdef MP
if(err.errMsg.indexOf('cancel') !== '-1'){
return;
}
uni.getSetting({
success: (res) => {
let authStatus = false;
switch (this.sourceTypeIndex) {
case 0:
authStatus = res.authSetting['scope.camera'];
break;
case 1:
authStatus = res.authSetting['scope.album'];
break;
case 2:
authStatus = res.authSetting['scope.album'] && res.authSetting['scope.camera'];
break;
default:
break;
}
if (!authStatus) {
uni.showModal({
title: '授权失败',
content: 'Hello uni-app需要从您的相机或相册获取图片,请在设置界面打开相关权限',
success: (res) => {
if (res.confirm) {
uni.openSetting()
}
}
})
}
},
})
// #endif
}
})
},
isFullImg: function() {
return new Promise((res) => {
uni.showModal({
content: "已经有9张图片了,是否清空现有图片?",
success: (e) => {
if (e.confirm) {
this.imageList = [];
res(true);
} else {
res(false)
}
},
fail: () => {
res(false)
}
})
})
},
previewImage: function(e) {
var current = e.target.dataset.src
uni.previewImage({
current: current,
urls: this.imageList
})
},
async checkPermission(code) {
let type = code ? code - 1 : this.sourceTypeIndex;
let status = permision.isIOS ? await permision.requestIOS(sourceType[type][0]) :
await permision.requestAndroid(type === 0 ? 'android.permission.CAMERA' :
'android.permission.READ_EXTERNAL_STORAGE');
if (status === null || status === 1) {
status = 1;
} else {
uni.showModal({
content: "没有开启权限",
confirmText: "设置",
success: function(res) {
if (res.confirm) {
permision.gotoAppSetting();
}
}
})
}
return status;
}
}
</script>
<style>
.cell-pd {
padding: 22rpx 30rpx;
}
.list-pd {
margin-top: 50rpx;
}
</style>
更多关于最新安卓SDK 3.94 版本在uni-app中拍照功能无法使用的实战教程也可以访问 https://www.itying.com/category-93-b0.html
更多关于最新安卓SDK 3.94 版本在uni-app中拍照功能无法使用的实战教程也可以访问 https://www.itying.com/category-93-b0.html
在 uni-app
中使用最新安卓 SDK 3.94 版本时,拍照功能无法使用,可能是由于以下几个原因导致的。下面是一些排查和解决方法:
1. 检查权限配置
确保在 AndroidManifest.xml
中已经正确配置了相机和存储权限:
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
在 uni-app
的 manifest.json
中,也需要声明相机权限:
"permission": {
"scope.camera": {
"desc": "需要访问相机"
}
}
2. 动态权限申请
在 Android 6.0 及以上版本,相机和存储权限需要动态申请。确保在调用拍照功能前,已经动态申请了相关权限。
可以使用 uni.authorize
或 uni.getSetting
来检查并申请权限:
uni.authorize({
scope: 'scope.camera',
success() {
console.log('相机权限已授权');
// 调用拍照功能
uni.chooseImage({
sourceType: ['camera'],
success(res) {
console.log('拍照成功', res);
},
fail(err) {
console.log('拍照失败', err);
}
});
},
fail() {
console.log('相机权限未授权');
}
});
3. 检查 SDK 版本兼容性
某些安卓 SDK 版本可能存在兼容性问题。可以尝试以下方法:
- 回退到之前稳定的 SDK 版本(如 3.93 或 3.92),查看问题是否解决。
- 检查官方文档或社区,确认 SDK 3.94 是否存在已知问题。
4. 检查 uni-app 插件
如果在 uni-app
中使用了第三方插件(如 uni-camera
或 uni-chooseImage
),确保插件与最新 SDK 兼容。可以尝试更新插件或检查插件的配置。
5. 调试日志
开启调试模式,查看控制台日志,检查是否有错误提示。例如:
uni.chooseImage({
sourceType: ['camera'],
success(res) {
console.log('拍照成功', res);
},
fail(err) {
console.log('拍照失败', err);
}
});