uni-app Android 11 调用plus.zip.compressImage方法压缩图片时 报传入参数错误

uni-app Android 11 调用plus.zip.compressImage方法压缩图片时 报传入参数错误

开发环境 版本号 项目创建方式
HbuilderX 3.1.6
示例代码:


return new Promise(function(resolve, reject) {
var file_name = src.substring(src.lastIndexOf('/')+1,src.length);
var file_type = file_name.substring(file_name.indexOf('.'));
var currentDate = new Date();
currentDate =Math.round(new Date().getTime())+Math.floor((Math.random()*1000)+1) ;
var file_compress_name = currentDate+file_type;
plus.zip.compressImage({
src: src,
dst: "_doc/" + file_compress_name,
format: 'jpg',
quality: quality
},
function(event) {
let tempPath = event.target;
console.log(event,'压缩成功')
resolve(tempPath)
},
function(error) {
console.log(error,'error')
reject(error);
});
})

操作步骤:

  • 使用手机拍照然后调用plus.zip.compressImage 进行压缩

预期结果:

  • 修改bug

实际结果:

  • 参数错误

bug描述: 小米10,OPPOreno5pro,小米11,一加8
Android 11环境
云打包或者使用自定义基座 ,不能本地环境直接运行
targetSdkVersion为29
使用原生组件拍照后,然后调用plus.zip.compressImage()进行图片压缩会报
{“code”:-1,“message”:“参数错误”}
使用uni.chooseImage从图库选择图片,然后调用plus.zip.compressImage()进行图片压缩就正确


更多关于uni-app Android 11 调用plus.zip.compressImage方法压缩图片时 报传入参数错误的实战教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复

楼主,问题有处理吗 ?

更多关于uni-app Android 11 调用plus.zip.compressImage方法压缩图片时 报传入参数错误的实战教程也可以访问 https://www.itying.com/category-93-b0.html


这是一个已知的Android 11兼容性问题。当targetSdkVersion≥29时,使用原生相机拍照返回的图片路径可能无法被plus.zip.compressImage直接识别。

问题原因:Android 11加强了文件存储权限管理,相机应用返回的URI路径格式发生了变化,导致压缩模块无法正确解析。

解决方案:

  1. 路径转换处理:在调用compressImage前,先将相机返回的路径转换为可访问的文件路径:
// 处理Android 11相机路径
if (src.startsWith('content://')) {
    // 使用plus.io转换路径
    plus.io.resolveLocalFileSystemURL(src, function(entry) {
        var validPath = entry.toLocalURL();
        // 使用validPath进行压缩
        compressImage(validPath);
    });
} else {
    // 直接使用原路径
    compressImage(src);
}
  1. 临时文件方案:将相机返回的图片先复制到应用私有目录:
plus.io.resolveLocalFileSystemURL(src, function(entry) {
    entry.file(function(file) {
        var reader = new plus.io.FileReader();
        reader.readAsDataURL(file);
        reader.onloadend = function(e) {
            // 将base64数据写入临时文件
            var tempPath = '_doc/temp_' + Date.now() + '.jpg';
            plus.io.resolveLocalFileSystemURL('_doc', function(dirEntry) {
                dirEntry.getFile(tempPath, {create: true}, function(fileEntry) {
                    fileEntry.createWriter(function(writer) {
                        writer.write(plus.io.convertLocalFileSystemURL(e.target.result));
                        writer.onwrite = function() {
                            // 使用tempPath进行压缩
                            compressImage(tempPath);
                        };
                    });
                });
            });
        };
    });
});
回到顶部