HarmonyOS systemShare系统分享图片不显示?

有一个网络图片icon,下载到本地,然后分享出去,但是分享面板预览图不显示图片,代码如下:

const contextFaker: Context = getContext(this);
let filePath = contextFaker.filesDir + '/exampleImage.jpg';
const fileUri = fileUri.getUriFromPath(filePath)
const data = new systemShare.SharedData({
  utd: utd.UniformDataType.IMAGE,
  uri: fileUri
})
let controller: systemShare.ShareController = new systemShare.ShareController(data);
// 获取UIAbility上下文对象
let context: common.UIAbilityContext = getContext() as common.UIAbilityContext;
controller.show(context, {
  previewMode: systemShare.SharePreviewMode.DETAIL, //详细预览图模式
  selectionMode: systemShare.SelectionMode.SINGLE
});
阅读 490
1 个回答

可参考以下demo,分享的文件需要存在应用的沙箱路径,建议将下载的图片放在cache目录中:

import { SaveImageResultVo, SaveImageUtil } from '@ohos/image';
import { common } from '@kit.AbilityKit';
import { BusinessError, request } from '@kit.BasicServicesKit';
import { BASE64_IMAGE_STR } from './Constants';
import { fileIo } from '@kit.CoreFileKit';
import { systemShare } from '@kit.ShareKit';
import { fileUri } from '@kit.CoreFileKit';
import { uniformTypeDescriptor as utd } from '@kit.ArkData';
@Entry
@Component
struct Index {
  private context = getContext(this) as common.UIAbilityContext

  build() {
    Column() {
      Button('保存网络图片到应用沙盒')
        .width('100%')
        .height(40)
        .fontSize(16)
        .fontWeight(FontWeight.Bold)
        .fontColor(Color.White)
        .onClick(async () => {
          console.debug(`image => 保存网络图片到应用沙盒`)
          let dirPath = this.context.cacheDir
          let fileName = 'temp2.jpg'
          if (fileIo.accessSync(`${dirPath}/${fileName}`)) {
            fileIo.rmdirSync(`${dirPath}/${fileName}`)
          }

          try {
            let result: SaveImageResultVo = await SaveImageUtil.saveImageToApplication(
             `http://gips0.***.com/it/u=3602773692,1512483864&fm=3028&app=3028&f=JPEG&fmt=auto?w=960&h=1280`
              , dirPath, fileName
            )
            if (0 == result.errorCode) {
              // 图片保存成功
              console.debug(`image => 保存图片到应用沙盒成功`)
              SaveImageUtil
                .saveImageToAlbum(`${dirPath}/${fileName}`)
                .then((result) => {
                  if (result) {
                    console.debug(`image => 保存图片到相册成功`)
                  } else {
                    console.debug(`image => 用户拒绝授权`)
                  }
                })
                .catch((error: BusinessError) => {
                  console.error('image => 保存图片到相册异常:')
                })
            }
          } catch (err) {
            console.error(err, 'image => 保存图片到应用沙盒异常:')
          }
        })

      Button('保存BASE64图片到应用沙盒')
        .width('100%')
        .height(40)
        .fontSize(16)
        .fontWeight(FontWeight.Bold)
        .fontColor(Color.White)
        .margin({ top: 15 })
        .onClick(() => {
          console.debug(`image => 保存网络图片到应用沙盒`)
          let dirPath = this.context.cacheDir
          let fileName = 'temp2.jpg'
          if (fileIo.accessSync(`${dirPath}/${fileName}`)) {
            fileIo.rmdirSync(`${dirPath}/${fileName}`)
          }
          SaveImageUtil
            .saveImageToApplication(BASE64_IMAGE_STR, dirPath, fileName)
            .then((data) => {
              if (0 == data.errorCode) {
                console.debug(`image => 保存图片到应用沙盒成功`)
                SaveImageUtil
                  .saveImageToAlbum(`${dirPath}/${fileName}`,'1500000100098361131_34b0496d-aa7d-454f-8b17-6bae6c09f811$2_原卷','png')
                  .then((result) => {
                    if (result) {
                      console.debug(`image => 保存图片到相册成功`)
                    } else {
                      console.debug(`image => 用户拒绝授权`)
                    }
                  })
                  .catch((error: BusinessError) => {
                    console.error('image => 保存图片到相册异常:')
                  })
              } else {
                console.debug(`image => 保存图片到应用沙盒失败:${data.errorCode}`)
              }
            })
            .catch((error: BusinessError) => {
              console.error('image => 保存图片到应用沙盒异常:')
            })
        })
      Button('share')
        .onClick(() => {
          const contextFaker: Context = getContext(this);
          let filePath = contextFaker.cacheDir + '/temp2.jpg';
          let fileUrl = fileUri.getUriFromPath(filePath)
          console.info('url',fileUrl,'path', filePath)
          const data = new systemShare.SharedData({
            utd: utd.UniformDataType.IMAGE,
            uri: fileUrl
          })
          let controller: systemShare.ShareController = new systemShare.ShareController(data);
          // 获取UIAbility上下文对象
          let context: common.UIAbilityContext = getContext() as common.UIAbilityContext;
          controller.show(context, {
            previewMode: systemShare.SharePreviewMode.DETAIL, //详细预览图模式
            selectionMode: systemShare.SelectionMode.SINGLE
          });
        })
    }
    .padding(15)
    .height('100%')
    .width('100%')
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进