uni-app 相机与选择图片闪退
uni-app 相机与选择图片闪退
示例代码:
const Select File = async () => {
try {
const res = await uni.chooseImage({
count: 9, // 允许最多选择9张图片
sizeType: ['original', 'compressed'],
sourceType: ['album', 'camera']
})
// 判断返回值有效性
if (!res || !res.tempFilePaths || res.tempFilePaths.length === 0) {
uni.showToast({
title: '未选择图片',
icon: 'none'
})
return
}
// 显示等待框
uni.showLoading({
title: '图片上传中...'
})
// 支持多图片上传
for (const filePath of res.tempFilePaths) {
await 上传文件(filePath)
}
// 全部上传完成后关闭等待框
uni.hideLoading()
} catch (error) {
uni.hideLoading()
console.error('选择文件失败:', error)
uni.showToast({
title: '选择图片失败',
icon: 'none'
})
}
}
操作步骤:
const Select File = async () => {
try {
const res = await uni.chooseImage({
count: 9, // 允许最多选择9张图片
sizeType: ['original', 'compressed'],
sourceType: ['album', 'camera']
})
// 判断返回值有效性
if (!res || !res.tempFilePaths || res.tempFilePaths.length === 0) {
uni.showToast({
title: '未选择图片',
icon: 'none'
})
return
}
// 显示等待框
uni.showLoading({
title: '图片上传中...'
})
// 支持多图片上传
for (const filePath of res.tempFilePaths) {
await 上传文件(filePath)
}
// 全部上传完成后关闭等待框
uni.hideLoading()
} catch (error) {
uni.hideLoading()
console.error('选择文件失败:', error)
uni.showToast({
title: '选择图片失败',
icon: 'none'
})
}
}
预期结果:
const Select File = async () => {
try {
const res = await uni.chooseImage({
count: 9, // 允许最多选择9张图片
sizeType: ['original', 'compressed'],
sourceType: ['album', 'camera']
})
// 判断返回值有效性
if (!res || !res.tempFilePaths || res.tempFilePaths.length === 0) {
uni.showToast({
title: '未选择图片',
icon: 'none'
})
return
}
// 显示等待框
uni.showLoading({
title: '图片上传中...'
})
// 支持多图片上传
for (const filePath of res.tempFilePaths) {
await 上传文件(filePath)
}
// 全部上传完成后关闭等待框
uni.hideLoading()
} catch (error) {
uni.hideLoading()
console.error('选择文件失败:', error)
uni.showToast({
title: '选择图片失败',
icon: 'none'
})
}
}
实际结果:
const Select File = async () => {
try {
const res = await uni.chooseImage({
count: 9, // 允许最多选择9张图片
sizeType: ['original', 'compressed'],
sourceType: ['album', 'camera']
})
// 判断返回值有效性
if (!res || !res.tempFilePaths || res.tempFilePaths.length === 0) {
uni.showToast({
title: '未选择图片',
icon: 'none'
})
return
}
// 显示等待框
uni.showLoading({
title: '图片上传中...'
})
// 支持多图片上传
for (const filePath of res.tempFilePaths) {
await 上传文件(filePath)
}
// 全部上传完成后关闭等待框
uni.hideLoading()
} catch (error) {
uni.hideLoading()
console.error('选择文件失败:', error)
uni.showToast({
title: '选择图片失败',
icon: 'none'
})
}
}
bug描述:
调用相机拍照与选择图片会闪退,相机闪退。
| 信息项 | 内容 |
|---|---|
| 产品分类 | uniapp/App |
| PC开发环境操作系统 | Windows |
| PC开发环境操作系统版本号 | win11 |
| HBuilderX类型 | 正式 |
| HBuilderX版本号 | 4.75 |
| 手机系统 | Android |
| 手机系统版本号 | Android 15 |
| 手机厂商 | 小米 |
| 手机机型 | 小米14 |
| 页面类型 | vue |
| vue版本 | vue3 |
| 打包方式 | 云端 |
| 项目创建方式 | HBuilderX |
更多关于uni-app 相机与选择图片闪退的实战教程也可以访问 https://www.itying.com/category-93-b0.html
2 回复
看代码没事问题,使用空白工程 标准基座试试
升级 HBuilderX 4.81 再试一下
更多关于uni-app 相机与选择图片闪退的实战教程也可以访问 https://www.itying.com/category-93-b0.html
根据你提供的代码和环境信息,uni-app相机与选择图片闪退的问题可能有以下几个原因:
- 权限配置问题:在Android平台上,使用相机和相册功能需要正确配置权限。请检查manifest.json中是否已添加以下权限:
{
"permissions": {
"CAMERA": {},
"READ_EXTERNAL_STORAGE": {},
"WRITE_EXTERNAL_STORAGE": {}
}
}
- Android 15兼容性问题:新版本Android系统对权限管理更加严格。建议在调用相机前动态申请权限:
// 检查并申请权限
const checkPermission = async () => {
const status = await uni.authorize({
scope: 'scope.camera'
})
if (status !== 'authorized') {
uni.showToast({
title: '需要相机权限',
icon: 'none'
})
}
}

