HarmonyOS鸿蒙Next中showAssetsCreationDialog显示沙箱图片空白,预览空白

HarmonyOS鸿蒙Next中showAssetsCreationDialog显示沙箱图片空白,预览空白 真机的这个问题什么时候能修复呢。。。

cke_557.png

cke_4179.png


更多关于HarmonyOS鸿蒙Next中showAssetsCreationDialog显示沙箱图片空白,预览空白的实战教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

开发者您好,

当用户需要保存图片、视频等用户文件到相册时,开发者可以调用showAssetsCreationDialog弹窗授权保存图片到相册。

注意:调用showAssetsCreationDialog接口时请确保module.json5文件中的abilities标签中配置了label和icon项,因为showAssetsCreationDialog依赖module.json5文件中的abilities标签中label和icon项,所以module.json5文件中的abilities标签必须存在label和icon项。module.json5的配置文件如下:

// module.json5的部分代码
"abilities": [
  {
    "name": "EntryAbility",
    "srcEntry": "./ets/entryability/EntryAbility.ets",
    "description": "$string:EntryAbility_desc",
    "icon": "$media:app_icon",
    // 以product的配置为主
    "label": "$string:app_name_test",
    "startWindowIcon": "$media:startIcon",
    "startWindowBackground": "$color:start_window_background",
    "exported": true,
    "skills": [
      {
        "entities": [
          "entity.system.home"
        ],
        "actions": [
          "action.system.home",
          "wxentity.action.open"
        ]
      }
    ]
  }
]

使用弹窗授权保存图片到相册,首先获取需要保存到媒体库的位于应用沙箱的图片/视频uri,然后调用showAssetsCreationDialog接口弹窗授权,通过fs.copyFileSync将图片保存到相册,示例代码如下:

import { fileIo as fs, fileUri } from '@kit.CoreFileKit'
import { common } from '@kit.AbilityKit';
import { photoAccessHelper } from '@kit.MediaLibraryKit';
import { camera, cameraPicker as picker } from '@kit.CameraKit';

@Entry
@Component
struct Index {
  @State message: string = 'SaveButton';

  async saveFile() {
    let mContext = getContext(this) as common.Context;
    let types: Array<picker.PickerMediaType> = [picker.PickerMediaType.PHOTO];
    let pickerProfile: picker.PickerProfile = {
      cameraPosition: camera.CameraPosition.CAMERA_POSITION_BACK,
      videoDuration: 15
    };
    let pickerResult: picker.PickerResult = await picker.pick(mContext,
      types, pickerProfile);
    if (pickerResult.resultCode === 0) {
      // 成功
      let finalUri = pickerResult.resultUri;
      // 保存图片到缓存目录
      let dirpath = (getContext(this) as common.UIAbilityContext).tempDir + '/cameTem.jpg';
      let dirUri = fileUri.getUriFromPath(dirpath);
      let finalFile = fs.openSync(finalUri, fs.OpenMode.READ_ONLY)
      let dirFile = fs.openSync(dirpath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE)
      fs.copyFileSync(finalFile.fd, dirFile.fd)
      fs.closeSync(finalFile);
      fs.closeSync(dirFile);
      console.info('ImagePicker', "Succeeded in copying. ");
      // 删除相册图片
      try {
        await photoAccessHelper.MediaAssetChangeRequest.deleteAssets(getContext(),
          [pickerResult.resultUri])
        finalUri = dirUri;
      } catch (err) {
        console.error('ImagePicker', `deleteAssetsDemo failed with error: ${err.code}, ${err.message}`);
      }
      console.info('ShowAssetsCreationDialogDemo.');
      // 获取需要保存到媒体库的位于应用沙箱的图片/视频uri
      try {
        let srcFileUris: Array<string> = [dirUri];
        let photoCreationConfigs: Array<photoAccessHelper.PhotoCreationConfig> = [{
          fileNameExtension: 'jpg',
          photoType: photoAccessHelper.PhotoType.IMAGE,
        }];
        let context = getContext();
        let phAccessHelper = photoAccessHelper.getPhotoAccessHelper(context);
        let desFileUris: Array<string> =
          await phAccessHelper.showAssetsCreationDialog(srcFileUris, photoCreationConfigs);
        console.info('showAssetsCreationDialog success, data is ' + desFileUris);
        try {
          let srcFile = fs.openSync(srcFileUris[0], fs.OpenMode.READ_ONLY)
          let desFile = fs.openSync(desFileUris[0], fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE)
          fs.copyFileSync(srcFile.fd, desFile.fd)
          fs.closeSync(srcFile);
          fs.closeSync(desFile);
        } catch (e) {
          console.log(JSON.stringify(e))
        }
      } catch (err) {
        console.error('showAssetsCreationDialog failed, errCode is ' + err.code + ', errMsg is ' + err.message);
      }
    }
  }

  build() {
    RelativeContainer() {
      Text(this.message)
        .id('SaveButton')
        .fontSize(50)
        .fontWeight(FontWeight.Bold)
        .alignRules({
          center: { anchor: '__container__', align: VerticalAlign.Center },
          middle: { anchor: '__container__', align: HorizontalAlign.Center }
        })
        .onClick(() => {
          this.saveFile();
        })
    }
    .height('100%')
    .width('100%')
  }
}

更多关于HarmonyOS鸿蒙Next中showAssetsCreationDialog显示沙箱图片空白,预览空白的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


感谢你的回复,但我的问题并不是实现的过程,而是图中showAssetsCreationDialog接口弹窗预览图片空白的问题,

在HarmonyOS鸿蒙Next中,showAssetsCreationDialog显示沙箱图片空白或预览空白的问题,可能由以下原因导致:

  1. 图片路径错误:确保图片路径正确,且图片已成功加载到沙箱中。
  2. 权限问题:检查应用是否具备访问沙箱图片的权限。
  3. 资源加载失败:确认图片资源是否已正确打包到应用中,且未损坏。
  4. UI渲染问题:检查UI组件是否正确配置,确保图片控件能够正常显示。

建议逐步排查上述问题,确保图片资源正确加载和显示。

回到顶部