harmony 鸿蒙保存媒体库资源

  • 2025-06-12
  • 浏览 (28)

保存媒体库资源

保存图片、视频等用户文件到图库时,无需申请相册管理模块权限’ohos.permission.WRITE_IMAGEVIDEO’,应用可以通过安全控件授权弹窗的方式,将用户指定的媒体资源保存到图库中。

获取支持保存的资源格式

下面以获取支持保存的图片类型资源格式为例。

开发步骤

调用phAccessHelper.getSupportedPhotoFormats接口获取支持保存的图片类型资源格式。

import photoAccessHelper from '@ohos.file.photoAccessHelper';
import { common } from '@kit.AbilityKit';

// 请在组件内获取context,确保this.getUiContext().getHostContext()返回结果为UIAbilityContext
let context: Context = this.getUIContext().getHostContext() as common.UIAbilityContext;
let phAccessHelper = photoAccessHelper.getPhotoAccessHelper(context);
@Entry
@Component

struct Index {
  @State outputText: string = '支持的类型为:\n';

  async function example(){
    try {
      this.outputText = '支持的类型为:\n';
      //参数为1表示获取支持的图片类型格式,参数为2表示获取支持的视频类型格式。
      let imageFormat  = await phAccessHelper.getSupportedPhotoFormats(1);
      let result = "";
      for (let i = 0; i < imageFormat.length; i++) {
        result += imageFormat[i];
        if (i !== imageFormat.length - 1) {
          result += ', ';
        }
      }
      this.outputText += result;
      console.info('getSupportedPhotoFormats success, data is ' + outputText);
    } catch (error) {
      console.error('getSupportedPhotoFormats failed, errCode is', error);
    }
  }

使用安全控件保存媒体库资源

安全控件的介绍可参考安全控件的保存控件

下面以使用安全控件创建一张图片资源为例。

开发步骤

  1. 设置安全控件按钮属性。
  2. 创建安全控件按钮。
  3. 调用MediaAssetChangeRequest.createImageAssetRequestPhotoAccessHelper.applyChanges接口创建图片资源。
import { photoAccessHelper } from '@kit.MediaLibraryKit';
import { common } from '@kit.AbilityKit';

@Entry
@Component
struct Index {
    saveButtonOptions: SaveButtonOptions = {
    icon: SaveIconStyle.FULL_FILLED,
    text: SaveDescription.SAVE_IMAGE,
    buttonType: ButtonType.Capsule
  } // 设置安全控件按钮属性。

  build() {
    Row() {
      Column() {
        SaveButton(this.saveButtonOptions) // 创建安全控件按钮。
          .onClick(async (event, result: SaveButtonOnClickResult) => {
             if (result == SaveButtonOnClickResult.SUCCESS) {
               try {
                 let context: Context = this.getUIContext().getHostContext() as common.UIAbilityContext;
                 let phAccessHelper = photoAccessHelper.getPhotoAccessHelper(context);
                 // 需要确保fileUri对应的资源存在。
                 let fileUri = 'file://com.example.temptest/data/storage/el2/base/haps/entry/files/test.jpg';
                 let assetChangeRequest: photoAccessHelper.MediaAssetChangeRequest = photoAccessHelper.MediaAssetChangeRequest.createImageAssetRequest(context, fileUri);
                 await phAccessHelper.applyChanges(assetChangeRequest);
                 console.info('createAsset successfully, uri: ' + assetChangeRequest.getAsset().uri);
               } catch (err) {
                 console.error(`create asset failed with error: ${err.code}, ${err.message}`);
               }
             } else {
               console.error('SaveButtonOnClickResult create asset failed');
             }
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}

除了上述通过fileUri从应用沙箱指定资源内容的方式,开发者还可以通过ArrayBuffer的方式添加资源内容,详情请参考addResource接口。

使用弹窗授权保存媒体库资源

下面以弹窗授权的方式保存一张图片资源为例。

开发步骤

  1. 指定待保存到媒体库的应用文件uri(需为应用沙箱路径)。
  2. 指定待保存照片的创建选项,包括文件后缀和照片类型,标题和照片子类型可选。
  3. 调用showAssetsCreationDialog,基于弹窗授权的方式获取的目标媒体文件uri。

弹框需要显示应用名称,无法直接获取应用名称,依赖于配置项的label和icon,因此调用此接口时请确保module.json5文件中的abilities标签中配置了label和icon项。当传入uri为沙箱路径时,可正常保存图片/视频,但无界面预览。 4. 将应用沙箱的照片内容写入媒体库的目标uri。

import { photoAccessHelper } from '@kit.MediaLibraryKit';
import { fileIo } from '@kit.CoreFileKit';
import { common } from '@kit.AbilityKit';

// 请在组件内获取context,确保this.getUiContext().getHostContext()返回结果为UIAbilityContext
let context: Context = this.getUIContext().getHostContext() as common.UIAbilityContext;
let phAccessHelper = photoAccessHelper.getPhotoAccessHelper(context);

async function example() {
  try {
    // 指定待保存到媒体库的位于应用沙箱的图片uri。
    let srcFileUri = 'file://com.example.temptest/data/storage/el2/base/haps/entry/files/test.jpg';
    let srcFileUris: Array<string> = [
      srcFileUri
    ];
    // 指定待保存照片的创建选项,包括文件后缀和照片类型,标题和照片子类型可选。
    let photoCreationConfigs: Array<photoAccessHelper.PhotoCreationConfig> = [
      {
        title: 'test', // 可选。
        fileNameExtension: 'jpg',
        photoType: photoAccessHelper.PhotoType.IMAGE,
        subtype: photoAccessHelper.PhotoSubtype.DEFAULT, // 可选。
      }
    ];
    // 基于弹窗授权的方式获取媒体库的目标uri。
    let desFileUris: Array<string> = await phAccessHelper.showAssetsCreationDialog(srcFileUris, photoCreationConfigs);
    // 将来源于应用沙箱的照片内容写入媒体库的目标uri。
    let desFile: fileIo.File = await fileIo.open(desFileUris[0], fileIo.OpenMode.WRITE_ONLY);
    let srcFile: fileIo.File = await fileIo.open(srcFileUri, fileIo.OpenMode.READ_ONLY);
    await fileIo.copyFile(srcFile.fd, desFile.fd);
    fileIo.closeSync(srcFile);
    fileIo.closeSync(desFile);
    console.info('create asset by dialog successfully');
  } catch (err) {
    console.error(`failed to create asset by dialog successfully errCode is: ${err.code}, ${err.message}`);
  }
}

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Media Library Kit(媒体文件管理服务)

harmony 鸿蒙使用MovingPhotoView播放动态照片

harmony 鸿蒙访问和管理动态照片资源

harmony 鸿蒙媒体资源变更通知相关指导

harmony 鸿蒙Media Library Kit 简介

harmony 鸿蒙使用Picker选择媒体库资源

harmony 鸿蒙开发准备

harmony 鸿蒙媒体资源使用指导

harmony 鸿蒙系统相册资源使用指导

harmony 鸿蒙用户相册资源使用指导

0  赞