HarmonyOS 实现保存图片到相册

使用官方的例子,报错 {"code":"14000011"}

这是官网例子网址:

https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/photoaccesshelper-useralbum-guidelines-V5#添加图片和视频到用户相册中

以下是我的源代码

import web_webview from '@ohos.web.webview';
import router from '@ohos.router'
import deviceInfo from '@ohos.deviceInfo';
import { DeviceUtils } from './DeviceUtils';
import geoLocationManager from '@ohos.geoLocationManager';
import abilityAccessCtrl from '@ohos.abilityAccessCtrl';
import common from '@ohos.app.ability.common';
import fs from '@ohos.file.fs';
// 相册相关
import image from '@ohos.multimedia.image';
import dataSharePredicates from '@ohos.data.dataSharePredicates';
import photoAccessHelper from '@ohos.file.photoAccessHelper';
import picker from '@ohos.file.picker';
import buffer from '@ohos.buffer';

import { lang } from '@kit.ArkTS';
import { ArkTSUtils } from '@kit.ArkTS'
import call from '@ohos.telephony.call';
import promptAction from '@ohos.promptAction';
import { BusinessError } from '@ohos.base';
import hilog from '@ohos.hilog';

import { bundleManager } from '@kit.AbilityKit'
// import { webviewController } from '../pages/WebviewPage'
//interface
import { ApplicationInfo, Location } from '../model/webviewModel'

const context = getContext(this);

//h5与原生交互方法
export class H5CallNativeMethods {
  webviewController: web_webview.WebviewController;
  // context: common.UIAbilityContext

  constructor(webviewController: web_webview.WebviewController, context: common.UIAbilityContext) {
    this.webviewController = webviewController
    // this.context = context
  }

  //回调单独声明 展示导航栏右侧按钮
  showTitleIcon() {
  }

  //原生分享
  share() {
  }

  //获取设备信息
  async getApplicationInfo() {
    let params: ApplicationInfo = {
      appVersion: "9.9.9", //原生app版本号
      deviceId: "udid", //设备ID
      model: "productSeries", //设备机型
      netWorkState: "wifi", //当前网络状态
      oem: "brand", //设备供应商
      osType: "distributionOSName", 
      osVersion: "distributionOSVersion", //系统平台版本
    }
    //获取应用信息
    let info =
      await bundleManager.getBundleInfoForSelf(bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION)
    params.appVersion = info.versionName

    //获取设备信息
    params.deviceId = await DeviceUtils.getDeviceId()
    params.model = deviceInfo.productSeries
    params.oem = deviceInfo.brand
    params.osType = deviceInfo.osFullName
    params.osVersion = deviceInfo.distributionOSReleaseType

    console.log('设备信息params', params)
    this.webviewController.runJavaScript(`fxtpApplicationInfoBack(${JSON.stringify(params)})`);
  }

  //发起跳转
  appRoute() {
  }

  //获取设备当前经纬度

  async getLocation() {
    let params: Location = {
      adcode: '', //获取adcode
      city: '', //获取城市
      country: '', //获取国家
      district: '', //获取区县
      latitude: '', //纬度(基础坐标)
      longitude: '', //经度(基础坐标)
      province: '', //获取省份
      street: '', //获取街道信息
      town: '', //获取乡镇信息
    }

    let atManager = abilityAccessCtrl.createAtManager()
    let requestInfo: geoLocationManager.CurrentLocationRequest = {
      'priority': geoLocationManager.LocationRequestPriority.FIRST_FIX, // 表示优先级信息--快速获取
      'scenario': geoLocationManager.LocationRequestScenario.UNSET, //表示场景信息
      'maxAccuracy': 0 //表示精度信息,单位是米
    };
    try {
      let status = await atManager.requestPermissionsFromUser(getContext(this), [
        'ohos.permission.LOCATION', 'ohos.permission.APPROXIMATELY_LOCATION'])
      console.log('status', JSON.stringify(status))
      if (status.authResults[0] === 0) { //保证精准定位开启
        let locationInfo = await geoLocationManager.getCurrentLocation(requestInfo)
        console.log('locationINfo', JSON.stringify(locationInfo))

        locationInfo['locale'] = 'zh'

        let reverseGeocodeRequest: geoLocationManager.ReverseGeoCodeRequest =
          { "latitude": 31.12, "longitude": 121.11, "maxItems": 1 };
        //转化坐标为具体的地址描述
        let addressFromLocation = await geoLocationManager.getAddressesFromLocation(reverseGeocodeRequest)
        console.log("addressFromLocation", JSON.stringify(addressFromLocation))
        params = {
          adcode: '', //获取adcode
          city: '', //获取城市
          country: '', //获取国家
          district: '', //获取区县
          latitude: '', //纬度(基础坐标)
          longitude: '', //经度(基础坐标)
          province: '', //获取省份
          street: '', //获取街道信息
          town: '', //获取乡镇信息
        }
        this.webviewController.runJavaScript(`fxtpLocationCallBack(${JSON.stringify(params)})`);
      }

    } catch (err) {
      console.log('TestLocation-err', JSON.stringify(err))
      this.webviewController.runJavaScript(`fxtpLocationCallBack(${JSON.stringify({})})`);
    }
  }

  //保存图片到相册
  async savedPhotosToAlbum(data: string) {
    let paramsFromH5 = JSON.parse(data) as Record<string, string>
    // let phAccessHelper = photoAccessHelper.getPhotoAccessHelper(this.context);
    let phAccessHelper = photoAccessHelper.getPhotoAccessHelper(context);
    let albumPredicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
    let albumName: photoAccessHelper.AlbumKeys = photoAccessHelper.AlbumKeys.ALBUM_NAME;
    albumPredicates.equalTo(albumName, 'albumName');
    let albumFetchOptions: photoAccessHelper.FetchOptions = {
      fetchColumns: [],
      predicates: albumPredicates
    };

    let photoPredicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
    let photoFetchOptions: photoAccessHelper.FetchOptions = {
      fetchColumns: [],
      predicates: photoPredicates
    };

    try {
      let albumFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, albumFetchOptions);
      let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject();
      console.info('getAlbums successfully, albumName: ' + album.albumName);
      let photoFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(photoFetchOptions);
      let photoAsset: photoAccessHelper.PhotoAsset = await photoFetchResult.getFirstObject();
      console.info('getAssets successfully, albumName: ' + photoAsset.displayName);


    } catch (err) {
      console.log('err', JSON.stringify(err))

    }

  }

  //调用登录和实名认证流程

  checkLogInAndAuthentication() {
  }

  //跳转系统浏览器
  openSystemBrowser() {
  }

  //隐藏原生导航栏

  hideNavigationBar() {
  }

  //获取当前登录用户信息

  getUserInfo() {
  }

  //推出原生Web容器

  closeWebController() {
    router.back()
  }

  //H5返回上一页方法
  goBackWebController() {
    let accessForward = this.webviewController.accessBackward();
    if (accessForward) {
      this.webviewController.backward()
    } else {
      //返回原生页面
      router.back()

    }
  }

  //H5退出登录并到首页

  exitLogin() {
  }

  //H5打开拨号界面

  callPhoneNumber(data: string = '{phone:}') {
    let paramsFromH5 = JSON.parse(data) as Record<string, string | number>
    if (paramsFromH5.phone) {
      call.makeCall(paramsFromH5.phone.toString())
    } else {
      promptAction.showToast({
        message: '调取拨号界面失败',
      });
    }


  }

  //隐藏原生状态栏

  controlStateHurdle() {
  }

  //h5保存数据到本地

  saveH5Data() {
  }

  //h5拿到保存到本地的数据

  getH5Data() {
  }

  //h5拉起人脸识别

  faceVerify() {
  }

  //商品详情页点击咨询跳转客服

  consultAction() {
  }

  //进去商品详情页传原生productId

  jumpProductDetail() {
  }

  //设置h5页面别名

  getH5Alias() {
  }

  //开启位置授权

  openLocation() {
  }

  //是否请求过位置授权

  openOriginLocation() {
  }

  //实名过期错误处理

  responseError() {
  }

  //隐藏h5头部返回按钮

  hideBackIcon() {
  }


  //在alias为syylj点击返回生效(此处为原生回调)

  fxtpClearH5Data() {
  }
}

export const
  methodList: string[] = [
    'showTitleIcon', //回调单独声明 展示导航栏右侧按钮
    'share', //原生分享
    'getApplicationInfo', //获取设备信息
    'appRoute', //发起跳转
    'newAppRoute', //新版跳转方法
    'getLocation', //获取设备当前经纬度
    'savedPhotosToAlbum', //保存图片到相册
    'checkLogInAndAuthentication', //调用登录和实名认证流程
    'openSystemBrowser', //跳转系
HarmonyOS
1天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
shlp
分享
微博
QQ
微信
回复
1天前
相关问题
HarmonyOS 保存图片本地相册
26浏览 • 1回复 待解决
HarmonyOS 保存图片系统相册
5浏览 • 0回复 待解决
HarmonyOS 保存图片相册问题
407浏览 • 1回复 待解决
HarmonyOS 截图保存图片相册
41浏览 • 1回复 待解决
怎么实现保存网络图片相册功能?
415浏览 • 1回复 待解决
HarmonyOS 保存图片相册权限问题
333浏览 • 1回复 待解决
HarmonyOS h5如何实现保存图片相册
3浏览 • 0回复 待解决
HarmonyOS保存图片系统相册问题咨询
687浏览 • 1回复 待解决
如何保存网络图片相册
770浏览 • 1回复 待解决
如何保存本地图片相册
1133浏览 • 1回复 待解决
求大佬告知如何保存图片相册
1176浏览 • 1回复 待解决
如何保存一张PNG图片相册
1882浏览 • 1回复 待解决
HarmonyOS H5页面保存图片本地相册
37浏览 • 1回复 待解决
HarmonyOS图片保存相册问题
471浏览 • 1回复 待解决
鸿蒙开发保存页面相册
6532浏览 • 1回复 待解决
HarmonyOS 图片保存相册报错
70浏览 • 1回复 待解决
HarmonyOS 相册保存图片设置title报错
404浏览 • 1回复 待解决