HarmonyOS 获取系统相册报错

let phAccessHelper = photoAccessHelper.getPhotoAccessHelper(getContext(this)); 
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 
let albumName: photoAccessHelper.AlbumKeys = photoAccessHelper.AlbumKeys.ALBUM_NAME; 
// predicates.equalTo(albumName,"label"); 
// let fetchOptions: photoAccessHelper.FetchOptions = { 
//   fetchColumns:[], 
//   predicates:predicates 
// } 
try{ 
  let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.SYSTEM,photoAccessHelper.AlbumSubtype.USER_GENERIC); 
  let album: photoAccessHelper.Album = await fetchResult.getFirstObject(); 
  console.log("获取相册成功,相册名称" + album.albumName); 
  fetchResult.close(); 
} catch (err){ 
  console.log("获取相册失败"+err); 
}

使用该代码获取相册内容 报错System inner fail。

HarmonyOS
2024-11-12 09:32:20
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
put_get

​可以先使用@ohos.file.photoAccessHelper (相册管理模块)API文档中的PhotoViewPicker的demo样例。

链接地址:

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-photoaccesshelper-V5

可以参考一下demo案例:​

import photoAccessHelper from '@ohos.file.photoAccessHelper'; 
import { abilityAccessCtrl, Permissions } from '@kit.AbilityKit'; 
import { BusinessError } from '@kit.BasicServicesKit'; 
 
const context = getContext(this); 
let phAccessHelper = photoAccessHelper.getPhotoAccessHelper(context); 
//申请权限 
const PERMISSIONS: Array<Permissions> = [ 
  'ohos.permission.READ_MEDIA', 
  'ohos.permission.WRITE_MEDIA', 
  'ohos.permission.READ_IMAGEVIDEO' 
]; 
 
@Entry 
@Component 
struct Index { 
  @State message: string = 'Hello World'; 
 
  build() { 
    Row() { 
      Column() { 
        Text(this.message) 
          .fontSize(50) 
          .fontWeight(FontWeight.Bold) 
      } 
      .width('50%') 
      Button("dasdas").onClick(()=>{ 
        example01() 
      }) 
    } 
    .height('100%') 
  } 
} 
 
async function example() { 
  try { 
    let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.VIDEO); 
    let album: photoAccessHelper.Album = await fetchResult.getFirstObject(); 
    console.info('get video album successfully, albumUri: ' + album.albumUri); 
    fetchResult.close(); 
  } catch (err) { 
    console.error('get video album failed with err: ' + err); 
  } 
} 
 
async function example01() { 
  try { 
    let PhotoSelectOptions = new photoAccessHelper.PhotoSelectOptions(); 
    PhotoSelectOptions.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE; 
    PhotoSelectOptions.maxSelectNumber = 5; 
    let photoPicker = new photoAccessHelper.PhotoViewPicker(); 
    photoPicker.select(PhotoSelectOptions).then((PhotoSelectResult: photoAccessHelper.PhotoSelectResult) => { 
      console.info('PhotoViewPicker.select successfully, PhotoSelectResult uri: ' + JSON.stringify(PhotoSelectResult)); 
    }).catch((err: BusinessError) => { 
      console.error(`PhotoViewPicker.select failed with err: ${err.code}, ${err.message}`); 
    }); 
  } catch (error) { 
    let err: BusinessError = error as BusinessError;

​1.访问系统相册,可以使用picker,也可以申请相册管理模块权限’ohos.permission.WRITE_IMAGEVIDEO’,picker是为了给一些没有权限的三方应用使用的,'ohos.permission.WRITE_IMAGEVIDEO’为系统等级,user_grant,实例代码中没有向用户动态授权的代码,向用户事情申请权限可参考一下文档:

https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/request-user-authorization-V5

普通应用申请系统级别权限,需要在应用市场ACL跨级别申请,具体参考:

https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/restricted-permissions-V5

https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/app-permission-mgmt-overview-V5

2.测试官网《系统相册资源使用指导》代码没有问题,可以正常获取:

https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/photoaccesshelper-systemalbum-guidelines-V5

3.指南中系统相册获取方法和API中@ohos.file.photoAccessHelper (相册管理模块)获取相册的方法区别是,系统相册可以在获取的时候弹出一个界面“是否允许获取相册”,api组件获取是组件调用了系统相册能力,用户无感知。​

分享
微博
QQ
微信
回复
2024-11-12 15:49:58
相关问题
HarmonyOS怎么获取系统相册的路径
411浏览 • 1回复 待解决
通过相册获取相册资源
788浏览 • 1回复 待解决
HarmonyOS 相册保存图片设置title报错
358浏览 • 1回复 待解决
HarmonyOS 如何打开系统相册页面
442浏览 • 1回复 待解决
HarmonyOS 应用中跳转系统相册
228浏览 • 1回复 待解决
HarmonyOS 系统相册权限访问问题
737浏览 • 1回复 待解决
HarmonyOS保存图片到系统相册问题咨询
638浏览 • 1回复 待解决
用户相册, 获取不到albumName
1762浏览 • 1回复 待解决
HarmonyOS如何获取用户相册所有目录?
2134浏览 • 1回复 待解决
HarmonyOS获取相册视频的宽高问题
462浏览 • 1回复 待解决
获取网络图片并保存到相册
1470浏览 • 1回复 待解决