diff --git a/code/DocsSample/ApplicationModels/MindSporeLiteArkTSDemo/README_zh.md b/code/DocsSample/ApplicationModels/MindSporeLiteArkTSDemo/README_zh.md index fb38a1bdc3c3b4beeb443d5ffad7048f9804a5a0..2b0a8c1723155b43fd5c89352611d779e760fdcc 100644 --- a/code/DocsSample/ApplicationModels/MindSporeLiteArkTSDemo/README_zh.md +++ b/code/DocsSample/ApplicationModels/MindSporeLiteArkTSDemo/README_zh.md @@ -48,7 +48,7 @@ entry ### 相关权限 -无。 +ohos.permission.READ_IMAGEVIDEO ### 依赖 diff --git a/code/DocsSample/ApplicationModels/MindSporeLiteArkTSDemo/entry/src/main/ets/pages/Index.ets b/code/DocsSample/ApplicationModels/MindSporeLiteArkTSDemo/entry/src/main/ets/pages/Index.ets index d3e2443f6c2e7ca57a04153e6e4772f71b0ab2f6..e2b95958342c30aab36002f99c6a22d38d6b5aa9 100644 --- a/code/DocsSample/ApplicationModels/MindSporeLiteArkTSDemo/entry/src/main/ets/pages/Index.ets +++ b/code/DocsSample/ApplicationModels/MindSporeLiteArkTSDemo/entry/src/main/ets/pages/Index.ets @@ -1,9 +1,28 @@ +/* +* Copyright (c) 2024-2025 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + import modelPredict from './model'; import { photoAccessHelper } from '@kit.MediaLibraryKit'; import { BusinessError } from '@ohos.base'; import image from '@ohos.multimedia.image'; import { resourceManager } from '@kit.LocalizationKit' import { fileIo } from '@kit.CoreFileKit'; +import { abilityAccessCtrl, Permissions } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; + +const PERMISSIONS: Permissions[] = ['ohos.permission.READ_IMAGEVIDEO']; @Entry @Component @@ -101,6 +120,19 @@ struct Index { 'Jewellery', 'Bridge', 'Cuisine', 'Bread', 'Caving', 'Shell', 'Wreath', 'Roof', 'Cookie', 'Canoe']; + aboutToAppear() { + abilityAccessCtrl.createAtManager().requestPermissionsFromUser(getContext(this), PERMISSIONS).then((data) => { + if (data.authResults[0] == 0) { + hilog.info(0xFF00, 'MindSporeLiteArkTSDemo', '%{public}s', 'request permission success'); + } else { + hilog.info(0xFF00, 'MindSporeLiteArkTSDemo', '%{public}s', 'user rejected'); + } + }).catch((err: BusinessError) => { + hilog.error(0xFF00, 'MindSporeLiteArkTSDemo', '%{public}s', + `request permission failed, error message: ${(err as BusinessError).message}`); + }); + } + build() { Row() { Column() { diff --git a/code/DocsSample/ApplicationModels/MindSporeLiteArkTSDemo/entry/src/main/module.json5 b/code/DocsSample/ApplicationModels/MindSporeLiteArkTSDemo/entry/src/main/module.json5 index 546532a6ebd885834aaddfa08190198e66f489d6..fcd2d37ece5edb6356de08fd29f184904f33adbb 100644 --- a/code/DocsSample/ApplicationModels/MindSporeLiteArkTSDemo/entry/src/main/module.json5 +++ b/code/DocsSample/ApplicationModels/MindSporeLiteArkTSDemo/entry/src/main/module.json5 @@ -1,5 +1,5 @@ /* -* Copyright (c) 2024 Huawei Device Co., Ltd. +* Copyright (c) 2024-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -47,6 +47,15 @@ } ] } + ], + "requestPermissions": [ + { + "name": "ohos.permission.READ_IMAGEVIDEO", + "reason": "$string:read_image_video_permission", + "usedScene": { + "when": "inuse" + } + } ] } } \ No newline at end of file diff --git a/code/DocsSample/ApplicationModels/MindSporeLiteArkTSDemo/entry/src/main/resources/base/element/string.json b/code/DocsSample/ApplicationModels/MindSporeLiteArkTSDemo/entry/src/main/resources/base/element/string.json index ad88d0d90f3338903dc92a9b554dc6d2e1db23d2..3923bded3d5b9a7e7bf9ed37031adc41332023f4 100644 --- a/code/DocsSample/ApplicationModels/MindSporeLiteArkTSDemo/entry/src/main/resources/base/element/string.json +++ b/code/DocsSample/ApplicationModels/MindSporeLiteArkTSDemo/entry/src/main/resources/base/element/string.json @@ -11,6 +11,10 @@ { "name": "EntryAbility_label", "value": "MindSporeLite" + }, + { + "name": "read_image_video_permission", + "value": "读取用户公共目录的图片或视频文件" } ] } \ No newline at end of file diff --git a/code/DocsSample/ApplicationModels/MindSporeLiteArkTSDemo/entry/src/main/resources/en_US/element/string.json b/code/DocsSample/ApplicationModels/MindSporeLiteArkTSDemo/entry/src/main/resources/en_US/element/string.json index ad88d0d90f3338903dc92a9b554dc6d2e1db23d2..add6e500dbbc293a4718131eb9310747154615d8 100644 --- a/code/DocsSample/ApplicationModels/MindSporeLiteArkTSDemo/entry/src/main/resources/en_US/element/string.json +++ b/code/DocsSample/ApplicationModels/MindSporeLiteArkTSDemo/entry/src/main/resources/en_US/element/string.json @@ -11,6 +11,10 @@ { "name": "EntryAbility_label", "value": "MindSporeLite" + }, + { + "name": "read_image_video_permission", + "value": "read image or video files from the user's public directory" } ] } \ No newline at end of file diff --git a/code/DocsSample/ApplicationModels/MindSporeLiteArkTSDemo/entry/src/main/resources/zh_CN/element/string.json b/code/DocsSample/ApplicationModels/MindSporeLiteArkTSDemo/entry/src/main/resources/zh_CN/element/string.json index 4831b337e09b3863a4efbb17d048fc78645d3cf1..f849a3e6336be556e4c5efc425c75cfd5b33782e 100644 --- a/code/DocsSample/ApplicationModels/MindSporeLiteArkTSDemo/entry/src/main/resources/zh_CN/element/string.json +++ b/code/DocsSample/ApplicationModels/MindSporeLiteArkTSDemo/entry/src/main/resources/zh_CN/element/string.json @@ -11,6 +11,10 @@ { "name": "EntryAbility_label", "value": "MindSporeLite" + }, + { + "name": "read_image_video_permission", + "value": "读取用户公共目录的图片或视频文件" } ] } \ No newline at end of file diff --git a/code/DocsSample/ApplicationModels/MindSporeLiteArkTSDemo/entry/src/ohosTest/ets/test/Ability.test.ets b/code/DocsSample/ApplicationModels/MindSporeLiteArkTSDemo/entry/src/ohosTest/ets/test/Ability.test.ets index 07e0d607c047bf1eb314c6c0738f533b9df2c60c..743b733fa34eb385d7a11c4d54544ceead83cbf5 100644 --- a/code/DocsSample/ApplicationModels/MindSporeLiteArkTSDemo/entry/src/ohosTest/ets/test/Ability.test.ets +++ b/code/DocsSample/ApplicationModels/MindSporeLiteArkTSDemo/entry/src/ohosTest/ets/test/Ability.test.ets @@ -1,3 +1,18 @@ +/* +* Copyright (c) 2024-2025 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + import { hilog } from '@kit.PerformanceAnalysisKit'; import { describe, it, expect } from '@ohos/hypium'; import Want from '@ohos.app.ability.Want'; @@ -35,6 +50,13 @@ export default function abilityTest() { expect(err).assertNull(); }) await driver.delayMs(3000); + // 用户授权 + await driver.assertComponentExist(ON.text( + await abilityDelegator.getAppContext().resourceManager.getStringValue($r('app.string.allow')))); + let permissionAllowBtn = await driver.findComponent(ON.text( + await abilityDelegator.getAppContext().resourceManager.getStringValue($r('app.string.allow')))); + await permissionAllowBtn.click(); + await driver.delayMs(1000); hilog.info(domain, TAG, BUNDLE + 'EntryAbility_001 end'); done(); }) @@ -72,7 +94,7 @@ export default function abilityTest() { await driver.assertComponentExist(ON.text('photo')); let msPhoto = await driver.findComponent(ON.text('photo')); await msPhoto.click(); - await driver.delayMs(1000); + await driver.delayMs(5000); // 点击照片,选中一张照片 let gridItems = await driver.findComponents(ON.type('GridItem')); await gridItems[1].click(); diff --git a/code/DocsSample/ApplicationModels/MindSporeLiteArkTSDemo/entry/src/ohosTest/resources/base/element/string.json b/code/DocsSample/ApplicationModels/MindSporeLiteArkTSDemo/entry/src/ohosTest/resources/base/element/string.json index 6b9a3b4953f09a19438794570d45b09e6adac893..c3f097d6be02cf3ff9f478f9d4803ab2c3fe3237 100644 --- a/code/DocsSample/ApplicationModels/MindSporeLiteArkTSDemo/entry/src/ohosTest/resources/base/element/string.json +++ b/code/DocsSample/ApplicationModels/MindSporeLiteArkTSDemo/entry/src/ohosTest/resources/base/element/string.json @@ -15,6 +15,10 @@ { "name": "completed", "value": "完成" + }, + { + "name": "allow", + "value": "允许" } ] } \ No newline at end of file diff --git a/code/DocsSample/ApplicationModels/MindSporeLiteCDemo/README_zh.md b/code/DocsSample/ApplicationModels/MindSporeLiteCDemo/README_zh.md index 4883ca633208e761977dd71a6771a14b917d33f3..23d9dd4a55b33ac0dfce2e4c1d2479c6cad25cdb 100644 --- a/code/DocsSample/ApplicationModels/MindSporeLiteCDemo/README_zh.md +++ b/code/DocsSample/ApplicationModels/MindSporeLiteCDemo/README_zh.md @@ -57,7 +57,7 @@ entry ### 相关权限 -无。 +ohos.permission.READ_IMAGEVIDEO ### 依赖 diff --git a/code/DocsSample/ApplicationModels/MindSporeLiteCDemo/entry/src/main/ets/pages/Index.ets b/code/DocsSample/ApplicationModels/MindSporeLiteCDemo/entry/src/main/ets/pages/Index.ets index b31b1e321f3f9808b741900450fdbaff807c5741..ca73d4c40e8f7fc8029933876900dff3637c8f04 100644 --- a/code/DocsSample/ApplicationModels/MindSporeLiteCDemo/entry/src/main/ets/pages/Index.ets +++ b/code/DocsSample/ApplicationModels/MindSporeLiteCDemo/entry/src/main/ets/pages/Index.ets @@ -1,11 +1,29 @@ +/* +* Copyright (c) 2024-2025 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + import resourceManager from '@ohos.resourceManager' import fileIo from '@ohos.file.fs'; import msliteNapi from 'libentry.so' import { photoAccessHelper } from '@kit.MediaLibraryKit'; import { BusinessError } from '@ohos.base'; import image from '@ohos.multimedia.image'; +import { abilityAccessCtrl, Permissions } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; const TAG = 'MindSporeLite'; +const PERMISSIONS: Permissions[] = ['ohos.permission.READ_IMAGEVIDEO']; @Entry @Component @@ -104,6 +122,19 @@ struct Index { 'Shell', 'Wreath', 'Roof', 'Cookie', 'Canoe' ]; + aboutToAppear() { + abilityAccessCtrl.createAtManager().requestPermissionsFromUser(getContext(this), PERMISSIONS).then((data) => { + if (data.authResults[0] == 0) { + hilog.info(0xFF00, TAG, '%{public}s', 'request permission success'); + } else { + hilog.info(0xFF00, TAG, '%{public}s', 'user rejected'); + } + }).catch((err: BusinessError) => { + hilog.error(0xFF00, TAG, '%{public}s', + `request permission failed, error message: ${(err as BusinessError).message}`); + }); + } + build() { Row() { Column() { diff --git a/code/DocsSample/ApplicationModels/MindSporeLiteCDemo/entry/src/main/module.json5 b/code/DocsSample/ApplicationModels/MindSporeLiteCDemo/entry/src/main/module.json5 index 546532a6ebd885834aaddfa08190198e66f489d6..fcd2d37ece5edb6356de08fd29f184904f33adbb 100644 --- a/code/DocsSample/ApplicationModels/MindSporeLiteCDemo/entry/src/main/module.json5 +++ b/code/DocsSample/ApplicationModels/MindSporeLiteCDemo/entry/src/main/module.json5 @@ -1,5 +1,5 @@ /* -* Copyright (c) 2024 Huawei Device Co., Ltd. +* Copyright (c) 2024-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -47,6 +47,15 @@ } ] } + ], + "requestPermissions": [ + { + "name": "ohos.permission.READ_IMAGEVIDEO", + "reason": "$string:read_image_video_permission", + "usedScene": { + "when": "inuse" + } + } ] } } \ No newline at end of file diff --git a/code/DocsSample/ApplicationModels/MindSporeLiteCDemo/entry/src/main/resources/base/element/string.json b/code/DocsSample/ApplicationModels/MindSporeLiteCDemo/entry/src/main/resources/base/element/string.json index 635edd86c207c1a1ca461da89213a85dfab22980..94723c4c5ab9609f947571a8bb87ae82bebaab03 100644 --- a/code/DocsSample/ApplicationModels/MindSporeLiteCDemo/entry/src/main/resources/base/element/string.json +++ b/code/DocsSample/ApplicationModels/MindSporeLiteCDemo/entry/src/main/resources/base/element/string.json @@ -15,6 +15,10 @@ { "name": "completed", "value": "完成" + }, + { + "name": "read_image_video_permission", + "value": "读取用户公共目录的图片或视频文件" } ] } \ No newline at end of file diff --git a/code/DocsSample/ApplicationModels/MindSporeLiteCDemo/entry/src/main/resources/en_US/element/string.json b/code/DocsSample/ApplicationModels/MindSporeLiteCDemo/entry/src/main/resources/en_US/element/string.json index 20a6d90b2379283759906cf2f1a320aaca78cb53..e589ea278bb96ddcd9a426ce56f5c1ab4fd9a11d 100644 --- a/code/DocsSample/ApplicationModels/MindSporeLiteCDemo/entry/src/main/resources/en_US/element/string.json +++ b/code/DocsSample/ApplicationModels/MindSporeLiteCDemo/entry/src/main/resources/en_US/element/string.json @@ -15,6 +15,10 @@ { "name": "completed", "value": "completed" + }, + { + "name": "read_image_video_permission", + "value": "read image or video files from the user's public directory" } ] } \ No newline at end of file diff --git a/code/DocsSample/ApplicationModels/MindSporeLiteCDemo/entry/src/main/resources/zh_CN/element/string.json b/code/DocsSample/ApplicationModels/MindSporeLiteCDemo/entry/src/main/resources/zh_CN/element/string.json index 3935b55bd70c2dd13265d7b0afb6027cd570ea10..b7a0c904915cc4c9520f8fac294ca529fe8f7efe 100644 --- a/code/DocsSample/ApplicationModels/MindSporeLiteCDemo/entry/src/main/resources/zh_CN/element/string.json +++ b/code/DocsSample/ApplicationModels/MindSporeLiteCDemo/entry/src/main/resources/zh_CN/element/string.json @@ -15,6 +15,10 @@ { "name": "completed", "value": "完成" + }, + { + "name": "read_image_video_permission", + "value": "读取用户公共目录的图片或视频文件" } ] } \ No newline at end of file diff --git a/code/DocsSample/ApplicationModels/MindSporeLiteCDemo/entry/src/ohosTest/ets/test/Ability.test.ets b/code/DocsSample/ApplicationModels/MindSporeLiteCDemo/entry/src/ohosTest/ets/test/Ability.test.ets index efd60bda6a9985f93809b2ec59d830802ba105b6..18c0d12f2cb483b2fb1b1f942b124b5b932f93b1 100644 --- a/code/DocsSample/ApplicationModels/MindSporeLiteCDemo/entry/src/ohosTest/ets/test/Ability.test.ets +++ b/code/DocsSample/ApplicationModels/MindSporeLiteCDemo/entry/src/ohosTest/ets/test/Ability.test.ets @@ -1,3 +1,18 @@ +/* +* Copyright (c) 2024-2025 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + import { hilog } from '@kit.PerformanceAnalysisKit'; import { describe, it, expect } from '@ohos/hypium'; import Want from '@ohos.app.ability.Want'; @@ -35,6 +50,13 @@ export default function abilityTest() { expect(err).assertNull(); }) await driver.delayMs(3000); + // 用户授权 + await driver.assertComponentExist(ON.text( + await abilityDelegator.getAppContext().resourceManager.getStringValue($r('app.string.allow')))); + let permissionAllowBtn = await driver.findComponent(ON.text( + await abilityDelegator.getAppContext().resourceManager.getStringValue($r('app.string.allow')))); + await permissionAllowBtn.click(); + await driver.delayMs(1000); hilog.info(DOMAIN, TAG, BUNDLE + 'EntryAbility_001 end'); done(); }) @@ -72,7 +94,7 @@ export default function abilityTest() { await driver.assertComponentExist(ON.text('photo')); let msPhoto = await driver.findComponent(ON.text('photo')); await msPhoto.click(); - await driver.delayMs(1000); + await driver.delayMs(5000); // chose a photo let gridItems = await driver.findComponents(ON.type('GridItem')); await gridItems[1].click(); diff --git a/code/DocsSample/ApplicationModels/MindSporeLiteCDemo/entry/src/ohosTest/resources/base/element/string.json b/code/DocsSample/ApplicationModels/MindSporeLiteCDemo/entry/src/ohosTest/resources/base/element/string.json index 6b9a3b4953f09a19438794570d45b09e6adac893..c3f097d6be02cf3ff9f478f9d4803ab2c3fe3237 100644 --- a/code/DocsSample/ApplicationModels/MindSporeLiteCDemo/entry/src/ohosTest/resources/base/element/string.json +++ b/code/DocsSample/ApplicationModels/MindSporeLiteCDemo/entry/src/ohosTest/resources/base/element/string.json @@ -15,6 +15,10 @@ { "name": "completed", "value": "完成" + }, + { + "name": "allow", + "value": "允许" } ] } \ No newline at end of file