!4649 【Sample】GamePuzzle下架medialibrary接口

Merge pull request !4649 from YangXin/GamePuzzle
This commit is contained in:
openharmony_ci 2024-08-01 02:15:54 +00:00 committed by Gitee
commit f7b20c5b0f
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
5 changed files with 26 additions and 43 deletions

View File

@ -29,6 +29,7 @@ VideoComponent/src/main/ets/components
| |---Index.ets // 首页
```
### 具体实现
+ 游戏中图片裁剪分割的效果实现在ImageModel中源码参考[ImageModel](entry/src/main/ets/model/ImageModel.ts):
+ 获取本地图片首先使用getMediaLibrary获取媒体库实例然后使用getFileAssets方法获取文件资源最后使用getAllObject获取检索结果中的所有文件资产方便展示
+ 裁剪图片准备:裁剪图片需要使用[@ohos.multimedia.image](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-image-kit/js-apis-image.md)接口,裁剪前需要申请图片编辑权限,使用[requestPermissionsFromUser](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-ability-kit/js-apis-abilityAccessCtrl.md#requestpermissionsfromuser9)申请,源码参考[Index.ets](entry/src/main/ets/pages/Index.ets);
@ -36,13 +37,10 @@ VideoComponent/src/main/ets/components
### 相关权限
[ohos.permission.READ_MEDIA](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/security/AccessToken/permissions-for-all.md#ohospermissionread_media)
[ohos.permission.WRITE_MEDIA](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/security/AccessToken/permissions-for-all.md#ohospermissionwrite_media)
[ohos.permission.MEDIA_LOCATION](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/security/AccessToken/permissions-for-all.md#ohospermissionmedia_location)
[ohos.permission.READ_IMAGEVIDEO](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/security/AccessToken/permissions-for-system-apps.md#ohospermissionread_imagevideo)
### 依赖
不涉及。

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2023 Huawei Device Co., Ltd.
* Copyright (c) 2022-2024 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
@ -24,8 +24,7 @@ import emitter from '@ohos.events.emitter';
import mediaquery from '@ohos.mediaquery';
import photoAccessHelper from '@ohos.file.photoAccessHelper';
const PERMISSIONS: Permissions[] = ['ohos.permission.READ_MEDIA', 'ohos.permission.WRITE_MEDIA',
'ohos.permission.MEDIA_LOCATION', 'ohos.permission.MANAGE_MISSIONS', 'ohos.permission.READ_IMAGEVIDEO'];
const PERMISSIONS: Permissions[] = ['ohos.permission.MEDIA_LOCATION', 'ohos.permission.MANAGE_MISSIONS', 'ohos.permission.READ_IMAGEVIDEO'];
const IMAGE_SIZE: number = px2vp(640)
const GAME_TIME: number = 300 // 游戏时长
const TAG: string = 'Index'

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2023 Huawei Device Co., Ltd.
* Copyright (c) 2022-2024 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
@ -57,20 +57,6 @@
}
],
"requestPermissions": [
{
"name": "ohos.permission.READ_MEDIA",
"reason": "$string:app_name",
"usedScene": {
"when": "always"
}
},
{
"name": "ohos.permission.WRITE_MEDIA",
"reason": "$string:app_name",
"usedScene": {
"when": "always"
}
},
{
"name": "ohos.permission.MEDIA_LOCATION",
"reason": "$string:app_name",

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Copyright (c) 2023-2024 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
@ -14,25 +14,25 @@
*/
import hilog from '@ohos.hilog';
import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium';
import { describe, it, expect } from '@ohos/hypium';
import AbilityDelegatorRegistry from '@ohos.app.ability.abilityDelegatorRegistry';
import { Driver, ON } from '@ohos.UiTest';
import dataSharePredicates from '@ohos.data.dataSharePredicates';
import mediaLibrary from '@ohos.multimedia.mediaLibrary';
import missionManager from '@ohos.app.ability.missionManager';
import emitter from '@ohos.events.emitter';
import userFileManager from '@ohos.filemanagement.userFileManager';
import dataSharePredicates from '@ohos.data.dataSharePredicates';
const DRIVER = Driver.create();
const BUNDLE = 'gamePuzzle';
let abilityDelegatorRegistry = AbilityDelegatorRegistry.getAbilityDelegator();
export default function abilityTest() {
describe('ActsAbilityTest', function () {
describe('ActsAbilityTest', () => {
/**
* 拉起应用
*/
it(`${BUNDLE}_StartAbility_001`, 0, async function (done) {
it(`${BUNDLE}_StartAbility_001`, 0, async (done: Function) => {
// Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function.
hilog.info(0xF811, 'testTag', 'StartAbility_001 begin');
try {
@ -51,7 +51,7 @@ export default function abilityTest() {
/**
* 授权
*/
it(`${BUNDLE}_GetPermission_001`, 0, async (done) => {
it(`${BUNDLE}_GetPermission_001`, 0, async (done: Function) => {
hilog.info(0xF811, 'testTag', 'GetPermission_001 begin');
await DRIVER.delayMs(2000);
let ability = abilityDelegatorRegistry.getAppContext();
@ -60,10 +60,6 @@ export default function abilityTest() {
let allowBtn = await DRIVER.findComponent(ON.text(await manager.getStringValue($r('app.string.allow'))));
await allowBtn.click();
await DRIVER.delayMs(1000);
await DRIVER.assertComponentExist(ON.text(await manager.getStringValue($r('app.string.allow'))));
let allowBtnSecond = await DRIVER.findComponent(ON.text(await manager.getStringValue($r('app.string.allow'))));
await allowBtnSecond.click();
await DRIVER.delayMs(1000);
done();
hilog.info(0xF811, 'testTag', 'GetPermission_001 end');
})
@ -71,16 +67,18 @@ export default function abilityTest() {
/**
* 决定是否调用相机拍照
*/
it(`${BUNDLE}_IsTakePhoto_001`, 0, async (done) => {
it(`${BUNDLE}_IsTakePhoto_001`, 0, async (done: Function) => {
hilog.info(0xF811, 'testTag', 'IsTakePhoto_001 begin');
let context = abilityDelegatorRegistry.getAppContext();
let manager = mediaLibrary.getMediaLibrary(context);
let albums = await manager.getFileAssets({
selections: mediaLibrary.FileKey.MEDIA_TYPE + '= ?',
selectionArgs: [mediaLibrary.MediaType.IMAGE.toString()]
});
let mgr = userFileManager.getUserFileMgr(context);
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let fetchOptions: userFileManager.FetchOptions = {
fetchColumns: [],
predicates: predicates.equalTo(userFileManager.ImageVideoKey.FILE_TYPE.toString(), userFileManager.FileType.IMAGE.toString())
};
let fetchResult: userFileManager.FetchResult<userFileManager.FileAsset> = await mgr.getPhotoAssets(fetchOptions);
// 小于两张,就拍照
if (albums.getCount() < 2) {
if (fetchResult.getCount() < 2) {
await abilityDelegatorRegistry.startAbility({
bundleName: 'com.ohos.camera',
abilityName: 'com.ohos.camera.MainAbility'
@ -102,7 +100,7 @@ export default function abilityTest() {
/**
* 重新吊起应用
*/
it(`${BUNDLE}_StartAbility_002`, 0, async (done) => {
it(`${BUNDLE}_StartAbility_002`, 0, async (done: Function) => {
hilog.info(0xF811, 'testTag', 'StartAbility_002 begin');
try {
await abilityDelegatorRegistry.startAbility({

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2023 Huawei Device Co., Ltd.
* Copyright (c) 2022-2024 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
@ -19,9 +19,11 @@ import hilog from '@ohos.hilog';
import { Hypium } from '@ohos/hypium';
import testsuite from '../test/List.test';
import window from '@ohos.window';
import Want from '@ohos.app.ability.Want';
import AbilityConstant from '@ohos.app.ability.AbilityConstant';
export default class TestAbility extends UIAbility {
onCreate(want, launchParam) {
onCreate(want :Want, launchParam: AbilityConstant.LaunchParam) {
hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility onCreate');
hilog.info(0x0000, 'testTag', '%{public}s', 'want param:' + JSON.stringify(want) ?? '');
hilog.info(0x0000, 'testTag', '%{public}s', 'launchParam:' + JSON.stringify(launchParam) ?? '');