微信小程序 拍照上传有时闪退
发布于 6 年前 作者 sdu 18988 次浏览 来自 问答

华为P9手机,小程序调用摄像头拍照–>确定–>完成。点击图片右上方的 完成 按钮后,有时能直接成功上传;有时会跳转到图片预览页面,这时再选择刚才拍摄的照片,点击右上方的 完成 按钮会直接闪退。如果不拍照,直接在预览页选择图片上传,则不会出现闪退。

代码如下:

wx.chooseImage({

count: 1,  //最多可以选择的图片总数  

sizeType: [‘compressed’], // 可以指定是原图还是压缩图,默认二者都有  

sourceType: [‘album’, ‘camera’], // 可以指定来源是相册还是相机,默认二者都有  

success: function (res) {

// 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片  

var tempFilePaths = res.tempFilePaths;

//启动上传等待中…  

wx.showToast({

title: ‘正在上传…’,

icon: ‘loading’,

mask: true,

duration: 10000

})

var uploadImgCount = 0;

for (var i = 0, h = tempFilePaths.length; i < h; i++) {

wx.uploadFile({

url: webUrl + ‘/uploadImg.html’,

filePath: tempFilePaths[uploadImgCount],

name: ‘file’,

formData: {

‘imgIndex’: uploadImgCount,

‘picName’: picName,

‘oper_id’: oepr_id,

‘pic_id’: pic_id,

‘pic_type’: 6,

‘pic_name’: pic_name,

‘pic_name2’: pic_name2,

‘pic_module’: pic_module

},

header: {

“Content-Type”: “multipart/form-data”

},

success: res => {

uploadImgCount++;

var data = JSON.parse(res.data);

//如果是最后一张,则隐藏等待中  

if (uploadImgCount == tempFilePaths.length) {

wx.hideToast();

}

if (data.success == ‘true’) {

//服务器返回格式: { “Catalog”: “testFolder”, “FileName”: “1.jpg”, “Url”: “https://test.com/1.jpg” }  

var real_img = data.real_img;

var pic_id = data.pic_id;

} else {

wx.showToast({

title: data.msg,

icon: ‘none’

})

}

},

fail: function (res) {

wx.hideToast();

wx.showModal({

title: ‘错误提示’,

content: ‘上传图片失败’,

showCancel: false,

success: function (res) { }

})

}

});

}

}

});

1 回复

你好,请提供一下出现问题的机型和微信版本,以及能复现问题的简单代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html)。

回到顶部