const compressFunAndroid = params => {
return new Promise(async (resolve, reject) => {
let info
try {
info = await getImageInfo(params.src)
} catch (err) {
reject('获取图片信息异常')
return
}
if (!info) {
reject('获取图片信息异常')
return
}
const maxSize = params.maxSize || 1080
const minSize = params.minSize || 640
if (info.width <= minSize && info.height <= minSize) {
resolve(params.src)
return
}
let newWidth = info.width
let newHeight = info.height
if (info.width > maxSize || info.height > maxSize) {
if (info.width > info.height) {
newHeight = Math.floor(info.height / (info.width / maxSize))
newWidth = maxSize
} else {
newWidth = Math.floor(info.width / (info.height / maxSize))
newHeight = maxSize
}
}
// Android 某些编解码链路对奇数尺寸 JPG 兼容较差,压缩输出尺寸规整为偶数像素。
const targetWidth = Math.max(2, newWidth % 2 === 0 ? newWidth : newWidth - 1)
const targetHeight = Math.max(2, newHeight % 2 === 0 ? newHeight : newHeight - 1)
try {
const result = await new Promise((resolveCompress, rejectCompress) => {
uni.compressImage({
src: params.src,
quality: params.quality * 100 || 90,
compressedWidth: targetWidth,
compressedHeight: targetHeight,
success: res => resolveCompress(res.tempFilePath),
fail: err => rejectCompress(err)
})
})
resolve(result)
} catch (error) {
console.error('Android compressImage fail:', error)
resolve(params.src)
}
})
} - 发布:2026-08-07 19:44
- 更新:2026-08-07 19:49
- 阅读:71
产品分类: uniapp/App
PC开发环境操作系统: Mac
PC开发环境操作系统版本号: 26.5.2 (25F84)
HBuilderX类型: 正式
HBuilderX版本号: 5.23
手机系统: Android
手机系统版本号: Android 16
手机厂商: OPPO
手机机型: 一加ace 2pro
页面类型: vue
vue版本: vue3
打包方式: 离线
项目创建方式: HBuilderX
示例代码:
操作步骤:
离线打包后,点击图片上传
离线打包后,点击图片上传
预期结果:
图片预览没有黑边
图片预览没有黑边
实际结果:
图片预览有黑边
图片预览有黑边

6***@qq.com (作者)
uni-app Android 离线打包场景,补充 previewImage 内置模块后,编译阶段报错:
D8: Unexpected error during rewriting of Kotlin metadata for class
uts.sdk.modules.DCloudUniPreviewImage.LongPressActionsOptions
该类属于 DCloud 内置 UTS 模块,存在兼容问题。
2026-08-07 20:21