uni-app 支付宝小程序在ios正常,在安卓手机上使用uni.downloadFile下载图片返回失效链接,是空白图片,这是怎么回事?
uni-app 支付宝小程序在ios正常,在安卓手机上使用uni.downloadFile下载图片返回失效链接,是空白图片,这是怎么回事?
开发环境 | 版本号 | 项目创建方式 |
---|---|---|
Windows | window10 | HBuilderX |
产品分类:
uniapp/小程序/阿里
PC开发环境操作系统:
Windows
PC开发环境操作系统版本号:
window10
HBuilderX类型:
正式
HBuilderX版本号:
3.4.7
第三方开发者工具版本号:
2.3.4
基础库版本号:
2.0
项目创建方式:
HBuilderX
示例代码:
header['tercode'] = store.state.uuid
header['version'] = '1'
header['authorization-token'] = store.state.token
uni.downloadFile({
url: completeUrl,
header: header,
success: (res) => {
if (res.tempFilePath) {
uni.getImageInfo({
src: res.tempFilePath,
success: function(res2) {
uni.showToast({
title: res2.path
})
uni.saveFile({
tempFilePath: res2.path,
success: function(res3) {
let fjImagePath = res3.savedFilePath
}
})
},
})
} else reject(new ApiError(-1, "下载失败:" + JSON.stringify(res)));
},
fail(err) {
reject(new ApiError(-1, "下载失败:" + err));
},
complete() {
uni.hideLoading();
}
})
操作步骤:
header['tercode'] = store.state.uuid
header['version'] = '1'
header['authorization-token'] = store.state.token
uni.downloadFile({
url: completeUrl,
header: header,
success: (res) => {
if (res.tempFilePath) {
uni.getImageInfo({
src: res.tempFilePath,
success: function(res2) {
uni.showToast({
title: res2.path
})
uni.saveFile({
tempFilePath: res2.path,
success: function(res3) {
let fjImagePath = res3.savedFilePath
}
})
},
})
} else reject(new ApiError(-1, "下载失败:" + JSON.stringify(res)));
},
fail(err) {
reject(new ApiError(-1, "下载失败:" + err));
},
complete() {
uni.hideLoading();
}
})
预期结果:
可以查看图片信息
实际结果:
getImageInfot出现错误 error:18,code:获取图片信息失败
bug描述:
【报Bug】支付宝小程序在ios是正常的,在 安卓手机上使用uni.downloadFile下载图片,返回的图片链接是失效的,是一个空白图片,这是怎么回事?使用uni.getImageInfo就一直显示错误 error:18,code:获取图片信息失败
更多关于uni-app 支付宝小程序在ios正常,在安卓手机上使用uni.downloadFile下载图片返回失效链接,是空白图片,这是怎么回事?的实战教程也可以访问 https://www.itying.com/category-93-b0.html
换个图片地址,试试,https:https://www.itying.com/uniimg.php?url=https://img-cdn-tc.dcloud.net.cn/uploads/avatar/000/73/57/99_avatar_mid.jpg?v=1652751980
更多关于uni-app 支付宝小程序在ios正常,在安卓手机上使用uni.downloadFile下载图片返回失效链接,是空白图片,这是怎么回事?的实战教程也可以访问 https://www.itying.com/category-93-b0.html
在uni-app中,支付宝小程序在iOS上正常,但在安卓手机上使用uni.downloadFile
下载图片时返回失效链接或空白图片,可能是由于以下几个原因导致的:
1. 文件路径问题
- 原因:在安卓设备上,文件路径的处理可能与iOS不同,导致文件下载后无法正确访问。
- 解决方案:确保下载后的文件路径是有效的,并且可以在安卓设备上正确访问。可以通过
uni.saveFile
将下载的文件保存到本地,然后再使用保存后的路径。
uni.downloadFile({
url: 'https://example.com/image.png',
success: (res) => {
if (res.statusCode === 200) {
uni.saveFile({
tempFilePath: res.tempFilePath,
success: (saveRes) => {
console.log('文件保存成功', saveRes.savedFilePath);
// 使用 saveRes.savedFilePath 访问文件
},
fail: (err) => {
console.error('文件保存失败', err);
}
});
}
},
fail: (err) => {
console.error('下载失败', err);
}
});
2. 文件权限问题
- 原因:安卓设备上可能存在文件权限问题,导致下载的文件无法被正确访问。
- 解决方案:确保在安卓设备上有足够的权限访问下载的文件。可以通过
uni.getFileSystemManager
来检查文件权限。
const fileManager = uni.getFileSystemManager();
fileManager.access({
path: res.tempFilePath,
success: () => {
console.log('文件可访问');
},
fail: (err) => {
console.error('文件不可访问', err);
}
});
3. 网络问题
- 原因:安卓设备上的网络环境可能与iOS不同,导致下载失败或下载的文件损坏。
- 解决方案:检查网络连接,确保在安卓设备上网络稳定。可以尝试在
uni.downloadFile
中添加超时设置。
uni.downloadFile({
url: 'https://example.com/image.png',
timeout: 10000, // 设置超时时间为10秒
success: (res) => {
if (res.statusCode === 200) {
console.log('下载成功', res.tempFilePath);
}
},
fail: (err) => {
console.error('下载失败', err);
}
});