当前位置:   article > 正文

HarmonyOS应用开发JSAPI—js获取相册权限调用相册图片_harmonyos访问相册中的照片

harmonyos访问相册中的照片

前置:

Api:8

语言:js开发

需要权限:

ohos.permission.READ_USER_STORAGE

ohos.permission.WRITE_USER_STORAGE

 

开始:

1.创建项目:

2.示例代码

test.hml

  1. <div class="container">
  2. <button class="title" onclick="getPhotograph">获取图片</button>
  3. <image src="{{ img }}" class="img"></image>
  4. </div>

test.css

  1. .container {
  2. flex-direction: column;
  3. justify-content: center;
  4. align-items: center;
  5. width: 100%;
  6. height: 100%;
  7. }
  8. .title {
  9. font-size: 50px;
  10. padding: 20px 30px;
  11. margin-top: 10px;
  12. }
  13. .img{
  14. width: 500px;
  15. height: 500px;
  16. border: 2px;
  17. margin-top: 10px;
  18. }

test.js

  1. import featureAbility from '@ohos.ability.featureAbility';
  2. import Want from '@ohos.application.Want';
  3. export default {
  4. data: {
  5. img:"",
  6. },
  7. // 获取权限,选择图片
  8. getPhotograph(){
  9. let that = this;
  10. let context = featureAbility.getContext();
  11. // let want = Want;
  12. let want = {
  13. "want": {
  14. "deviceId": "",
  15. "bundleName": "",
  16. "abilityName": "",
  17. "uri": "",
  18. "type": "image/*",
  19. "action": "android.intent.action.GET_CONTENT",
  20. "flags":"",
  21. "parameters":{},
  22. "entities":[]
  23. },
  24. };
  25. context.requestPermissionsFromUser(
  26. [
  27. "ohos.permission.READ_USER_STORAGE",
  28. "ohos.permission.WRITE_USER_STORAGE"
  29. ], 1, (err, data) => {
  30. if(err){
  31. console.info( "code" + JSON.stringify(err.code));
  32. console.info( "stack" + JSON.stringify(err.stack));
  33. console.info( "message" + JSON.stringify(err.message));
  34. console.info( "name" + JSON.stringify(err.name));
  35. }else{
  36. console.info("authResults==>:" + JSON.stringify(data.authResults));
  37. console.info("permissions==>:" + JSON.stringify(data.permissions));
  38. console.info("requestCode==>:" + JSON.stringify(data.requestCode));
  39. //跳转到相册界面
  40. featureAbility.startAbilityForResult(want, (error, data) => {
  41. if (error) {
  42. console.error('Operation failed. message: ' + error.message);
  43. console.error('Operation failed. name: ' + error.name);
  44. console.error('Operation failed. stack: ' + error.stack);
  45. console.error('Operation failed. code: ' + error.code);
  46. return;
  47. }
  48. if (data.resultCode == -1) { //返回到该界面并且得到了结果
  49. console.info('succeeded: ' + JSON.stringify(data));
  50. console.info('Operation uri: ' + JSON.stringify(data.want.uri));
  51. that.img = data.want.uri
  52. } else { //返回到该界面没有选择相片
  53. console.info('没有选择图片: ');
  54. }
  55. });
  56. }
  57. })
  58. }
  59. }

3.效果如图:

4.代码地址

HarmonyOSAPP开发相关组件: 深圳市蛟龙腾飞网络科技有限公司 - Gitee.com

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/234672
推荐阅读
相关标签
  

闽ICP备14008679号