HarmonyOS鸿蒙Next中system.share分享图片无法显示您的文件不支持分享

HarmonyOS鸿蒙Next中system.share分享图片无法显示您的文件不支持分享 09-02 09:37:19.923 8505-8505 A03d00/JSAPP com.examp…lication I —file://com.example.myapplication/data/storage/el2/base/haps/CloudMuSIC/files/test1.png

09-02 09:37:19.925 8505-8505 A03d00/JSAPP com.examp…lication I —false

发现文件无法读取 cke_4341.png 这个是文件目录 文件是存在,测试了文本分享,是正常的,查询资料要通过官方建议的方法获取文件uri,发现还是存在这个问题

private async handelShare(): Promise<void> {
  const contextFaker: Context = getContext(this);
  let filePath = contextFaker.filesDir
  const resMgr: resourceManager.ResourceManager = contextFaker.resourceManager;
  let pathSource = "test1.png"
  console.log("---"+fileUri.getUriFromPath(`${filePath}`));
  console.log("---"+await fs.access(fileUri.getUriFromPath(`${filePath}/${pathSource}`)));
  let utdTypeId = utd.getUniformDataTypeByFilenameExtension('.jpg', utd.UniformDataType.IMAGE);
  let shareData: systemShare.SharedData = new systemShare.SharedData({
    utd: utdTypeId,
    uri: fileUri.getUriFromPath(`${filePath}/${pathSource}`).toString(),
    label:"IMAGE",
    title: 'Picture Title',
    description: 'Picture Description',
    thumbnail: await resMgr.getMediaContent($r('app.media.test3'))
  });
  let controller: systemShare.ShareController = new systemShare.ShareController(shareData);
  let context = getContext(this) as common.UIAbilityContext;
  controller.show(context, {
    previewMode: systemShare.SharePreviewMode.DETAIL,
    selectionMode: systemShare.SelectionMode.BATCH,
  }).then(() => {
    console.info('HuaweiShare_ show');
  }).catch((error: BusinessError) => {
    console.error(`HuaweiShare_ show error. Code: ${error.code}, message: ${error.message}`);
  });
}

附上源代码


更多关于HarmonyOS鸿蒙Next中system.share分享图片无法显示您的文件不支持分享的实战教程也可以访问 https://www.itying.com/category-93-b0.html

7 回复

【背景知识】 分享图片:图片类型分享支持将一张或多张图片分享到目标设备/目标应用。

【参考方案】: 可参考截屏事件监听及分享示例,实现了监听系统截屏事件的功能,并支持截图保存到相册和分享。

  1. 通过systemShare实现图片分享。
// 获取精准的utd类型
let utdTypeId = utd.getUniformDataTypeByFilenameExtension('.png', utd.UniformDataType.IMAGE);
let shareData: systemShare.SharedData = new systemShare.SharedData({
  utd: utdTypeId,
  uri: fileUri.getUriFromPath(filePath),
  title: '图片标题', // 不传title字段时,显示图片文件名
  description: '图片描述', // 不传description字段时,显示图片大小
  // thumbnail: new Uint8Array() // 优先使用传递的缩略图预览  不传则默认使用原图做预览图
});
// 进行分享面板显示
let controller: systemShare.ShareController = new systemShare.ShareController(shareData);
controller.show(context, {
  selectionMode: systemShare.SelectionMode.SINGLE,
  previewMode: systemShare.SharePreviewMode.DETAIL,
})

更多关于HarmonyOS鸿蒙Next中system.share分享图片无法显示您的文件不支持分享的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


重要设置是这个 selectionMode: systemShare.SelectionMode.SINGLE 吗,

对,看文档使用了getUriFromPath但是还是出现这个问题,

有大佬帮忙看一下吗

HarmonyOS Next的system.share接口分享图片时出现"不支持分享"提示,通常由以下原因导致:

  1. 文件格式不支持:检查图片是否为PNG/JPEG/WEBP等鸿蒙支持的格式
  2. 文件路径无效:确保使用的是有效的沙箱路径或通过Picker获取的合法URI
  3. 权限未配置:在module.json5中已声明ohos.permission.READ_MEDIA权限
  4. 文件不存在:确认指定路径下的图片文件实际存在且可读

建议使用MediaLibrary接口获取有效的媒体文件URI进行分享操作。

在HarmonyOS Next中,通过system.share分享图片时出现"无法显示您的文件不支持分享"的问题,通常是由于文件URI格式或权限配置不正确导致的。根据您提供的代码和日志,以下是关键排查点:

  1. URI格式问题
    使用fileUri.getUriFromPath()生成的URI需要符合HarmonyOS的URI规范。确保路径拼接正确,且文件实际存在。日志显示file://com.example.myapplication/.../test1.png,但fs.access返回false,表明文件可能无法访问。建议使用fs.statfs.access确认文件可读性。

  2. 文件权限配置
    module.json5中,需声明requestPermissions权限(如ohos.permission.READ_MEDIA)和abilitiesuriPermissions(允许分享组件访问文件URI)。示例配置:

    {
      "module": {
        "requestPermissions": [{
          "name": "ohos.permission.READ_MEDIA",
          "reason": "用于分享图片文件"
        }],
        "abilities": [{
          "uriPermissions": [{
            "mode": "readWrite",
            "type": "path",
            "path": "files"
          }]
        }]
      }
    }
    
  3. UTD类型匹配
    代码中使用.jpg的UTD类型(utd.getUniformDataTypeByFilenameExtension('.jpg', ...)),但文件扩展名为.png。需保持一致:

    let utdTypeId = utd.getUniformDataTypeByFilenameExtension('.png', utd.UniformDataType.IMAGE);
    
  4. URI转换为字符串
    fileUri.getUriFromPath()返回的是Uri对象,直接调用.toString()可能未标准化。建议使用uri.toString()前验证URI有效性:

    const uri = fileUri.getUriFromPath(`${filePath}/${pathSource}`);
    console.log("URI:", uri.toString()); // 检查输出是否符合预期
    
  5. 上下文获取
    确保getContext(this)返回的是有效的UIAbilityContext,且文件路径基于该上下文(如context.filesDir)。日志中的路径el2/base/haps/...可能涉及沙箱限制,建议使用官方API(如@ohos.file.fs)管理文件。

总结:优先检查文件权限配置和UTD类型匹配,确保URI可被分享组件正确解析。若问题仍存在,可尝试使用@ohos.app.ability.Want配合startAbility的分享方案作为备选。

回到顶部