uni-app HBX4.36官方内置的uni.shareWithSystem分享提示获取资源失败

uni-app HBX4.36官方内置的uni.shareWithSystem分享提示获取资源失败

代码如下严格按照UNIAPPX的API文档来的,一直获取资源失败,资源是存在没有问题的。点击分享弹出来后比如要分享到微信就会提示:获取资源失败。

const pathList:string = "/storage/emulated/0/Music/lv_0_20240925082088.mp3"  
uni.shareWithSystem({  
    filePaths: [pathList],  
    type:'file',  
    success(res) {  
        console.log(res)  
        console.log('success')  
    },  
    fail(res) {  
        console.log('Share failed, ' + "res.errCode =" + res.errCode + '---res.errMsg= ' + res.errMsg)  
        uni.showToast({  
            icon: "error",  
            title: "errorCode=" + res.errCode  
        })  
    }  
})

打印出来是:

//‍[⁠uts.sdk.modules.DCloudUniShareWithSystem.ShareWithSystemSuccess⁠]‍   
{}

更多关于uni-app HBX4.36官方内置的uni.shareWithSystem分享提示获取资源失败的实战教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复

请官方修正一下也可以分享外部储存路径的文件。

更多关于uni-app HBX4.36官方内置的uni.shareWithSystem分享提示获取资源失败的实战教程也可以访问 https://www.itying.com/category-93-b0.html


在处理 uni-appuni.shareWithSystem 功能时,如果遇到“获取资源失败”的提示,这通常意味着在尝试分享时,系统无法正确获取或解析你要分享的资源。这可能是因为资源路径不正确、资源文件不存在、资源格式不支持或其他相关原因。

以下是一个简单的代码示例,演示如何使用 uni.shareWithSystem 分享图片资源,并附带一些可能的错误处理逻辑,以确保资源能够被正确加载和分享。

示例代码

// 假设你有一个图片资源的本地路径
const imagePath = '/static/images/share-image.png';

// 使用uni.getImageInfo获取图片信息,确保图片存在且路径正确
uni.getImageInfo({
    src: imagePath,
    success: (res) => {
        // 图片信息获取成功,尝试分享
        uni.shareWithSystem({
            itemList: [
                {
                    type: 'image', // 分享类型,这里是图片
                    data: res.path, // 图片的本地路径
                }
            ],
            success: () => {
                console.log('分享成功');
            },
            fail: (err) => {
                console.error('分享失败:', err);
                // 错误处理,可以根据错误码或错误信息进一步处理
                if (err.errMsg === 'shareWithSystem:fail get resource failed') {
                    uni.showToast({
                        title: '获取资源失败,请检查图片路径',
                        icon: 'none'
                    });
                } else {
                    uni.showToast({
                        title: '分享失败,错误详情:' + err.errMsg,
                        icon: 'none'
                    });
                }
            }
        });
    },
    fail: (err) => {
        // 图片信息获取失败,通常是因为图片路径不正确或图片不存在
        console.error('获取图片信息失败:', err);
        uni.showToast({
            title: '图片不存在或路径错误',
            icon: 'none'
        });
    }
});

注意事项

  1. 资源路径:确保 imagePath 是正确的本地路径。如果资源是网络图片,你需要先将其下载到本地。
  2. 图片格式:检查图片格式是否被系统支持。通常,JPEG和PNG格式是被广泛支持的。
  3. 权限问题:确保应用有访问和读取该资源的权限。
  4. 调试:使用 console.loguni.showToast 来调试和显示错误信息,帮助定位问题。

通过上述代码和注意事项,你应该能够更有效地诊断和解决 uni.shareWithSystem 分享时遇到的“获取资源失败”问题。

回到顶部