赞
踩
Api:8
语言:js开发
需要权限:
ohos.permission.READ_USER_STORAGE
ohos.permission.WRITE_USER_STORAGE
1.创建项目:
2.示例代码
test.hml
- <div class="container">
- <button class="title" onclick="getPhotograph">获取图片</button>
- <image src="{{ img }}" class="img"></image>
- </div>
test.css
- .container {
- flex-direction: column;
- justify-content: center;
- align-items: center;
- width: 100%;
- height: 100%;
- }
-
- .title {
- font-size: 50px;
- padding: 20px 30px;
- margin-top: 10px;
- }
- .img{
- width: 500px;
- height: 500px;
- border: 2px;
- margin-top: 10px;
- }
test.js
- import featureAbility from '@ohos.ability.featureAbility';
- import Want from '@ohos.application.Want';
- export default {
- data: {
- img:"",
- },
- // 获取权限,选择图片
- getPhotograph(){
- let that = this;
- let context = featureAbility.getContext();
- // let want = Want;
- let want = {
- "want": {
- "deviceId": "",
- "bundleName": "",
- "abilityName": "",
- "uri": "",
- "type": "image/*",
- "action": "android.intent.action.GET_CONTENT",
- "flags":"",
- "parameters":{},
- "entities":[]
- },
- };
- context.requestPermissionsFromUser(
- [
- "ohos.permission.READ_USER_STORAGE",
- "ohos.permission.WRITE_USER_STORAGE"
- ], 1, (err, data) => {
- if(err){
- console.info( "code" + JSON.stringify(err.code));
- console.info( "stack" + JSON.stringify(err.stack));
- console.info( "message" + JSON.stringify(err.message));
- console.info( "name" + JSON.stringify(err.name));
- }else{
- console.info("authResults==>:" + JSON.stringify(data.authResults));
- console.info("permissions==>:" + JSON.stringify(data.permissions));
- console.info("requestCode==>:" + JSON.stringify(data.requestCode));
- //跳转到相册界面
- featureAbility.startAbilityForResult(want, (error, data) => {
- if (error) {
- console.error('Operation failed. message: ' + error.message);
- console.error('Operation failed. name: ' + error.name);
- console.error('Operation failed. stack: ' + error.stack);
- console.error('Operation failed. code: ' + error.code);
- return;
- }
- if (data.resultCode == -1) { //返回到该界面并且得到了结果
- console.info('succeeded: ' + JSON.stringify(data));
- console.info('Operation uri: ' + JSON.stringify(data.want.uri));
- that.img = data.want.uri
- } else { //返回到该界面没有选择相片
- console.info('没有选择图片: ');
- }
- });
- }
-
- })
-
- }
- }
-
3.效果如图:
4.代码地址
HarmonyOSAPP开发相关组件: 深圳市蛟龙腾飞网络科技有限公司 - Gitee.com
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。