diff --git a/zh-cn/application-dev/file-management/medialibrary-album-guidelines.md b/zh-cn/application-dev/file-management/medialibrary-album-guidelines.md index 60043767345181ea2a247fd8b30ac155b7993d0c..0a697df0dc233a9a1500e868bde802aa7371976d 100644 --- a/zh-cn/application-dev/file-management/medialibrary-album-guidelines.md +++ b/zh-cn/application-dev/file-management/medialibrary-album-guidelines.md @@ -39,19 +39,19 @@ mediaLibrary提供相册相关的接口,供开发者创建、删除相册, ```ts async function example() { - let mediaType = mediaLibrary.MediaType.IMAGE; - let DIR_IMAGE = mediaLibrary.DirectoryType.DIR_IMAGE; - const context = getContext(this); - let media = mediaLibrary.getMediaLibrary(context); - const path = await media.getPublicDirectory(DIR_IMAGE); - //myAlbum为新建文件保存路径,也是新建相册的名称 - media.createAsset(mediaType, 'test.jpg', path + 'myAlbum/', (err, fileAsset) => { - if (fileAsset === undefined) { - console.error('createAlbum failed, message = ' + err); - } else { - console.info('createAlbum successfully, message = ' + JSON.stringify(fileAsset)); - } - }); + let mediaType = mediaLibrary.MediaType.IMAGE; + let DIR_IMAGE = mediaLibrary.DirectoryType.DIR_IMAGE; + const context = getContext(this); + let media = mediaLibrary.getMediaLibrary(context); + const path = await media.getPublicDirectory(DIR_IMAGE); + //myAlbum为新建文件保存路径,也是新建相册的名称 + media.createAsset(mediaType, 'test.jpg', path + 'myAlbum/', (err, fileAsset) => { + if (fileAsset === undefined) { + console.error('createAlbum failed, message = ' + err); + } else { + console.info('createAlbum successfully, message = ' + JSON.stringify(fileAsset)); + } + }); } ``` @@ -75,20 +75,20 @@ async function example() { ```ts async function example() { - let AlbumNoArgsfetchOp = { - selections: '', - selectionArgs: [], - }; - const context = getContext(this); - let media = mediaLibrary.getMediaLibrary(context); - let albumList = await media.getAlbums(AlbumNoArgsfetchOp); - let album = albumList[0]; - album.albumName = 'newAlbum'; - //回调返回空 - album.commitModify().then(() => { - console.info("albumRename successfully"); - }).catch((err) => { - console.error("albumRename failed with error: " + err); - }); + let AlbumNoArgsfetchOp = { + selections: '', + selectionArgs: [], + }; + const context = getContext(this); + let media = mediaLibrary.getMediaLibrary(context); + let albumList = await media.getAlbums(AlbumNoArgsfetchOp); + let album = albumList[0]; + album.albumName = 'newAlbum'; + //回调返回空 + album.commitModify().then(() => { + console.info("albumRename successfully"); + }).catch((err) => { + console.error("albumRename failed with error: " + err); + }); } ``` diff --git a/zh-cn/application-dev/file-management/medialibrary-filepath-guidelines.md b/zh-cn/application-dev/file-management/medialibrary-filepath-guidelines.md index b42db693e0fe55e97061356d5040e4ff0b2cab0a..9c6475e65ea02b0e62a3eb4578c1a81921eab6d6 100644 --- a/zh-cn/application-dev/file-management/medialibrary-filepath-guidelines.md +++ b/zh-cn/application-dev/file-management/medialibrary-filepath-guidelines.md @@ -36,15 +36,15 @@ Openharmony上用户数据统一由媒体库进行管理,用户数据用户数 ```ts async function example(){ - const context = getContext(this); - let media = mediaLibrary.getMediaLibrary(context); - let DIR_CAMERA = mediaLibrary.DirectoryType.DIR_CAMERA; - const dicResult = await media.getPublicDirectory(DIR_CAMERA); - if (dicResult == 'Camera/') { - console.info('mediaLibraryTest : getPublicDirectory passed'); - } else { - console.error('mediaLibraryTest : getPublicDirectory failed'); - } + const context = getContext(this); + let media = mediaLibrary.getMediaLibrary(context); + let DIR_CAMERA = mediaLibrary.DirectoryType.DIR_CAMERA; + const dicResult = await media.getPublicDirectory(DIR_CAMERA); + if (dicResult == 'Camera/') { + console.info('mediaLibraryTest : getPublicDirectory passed'); + } else { + console.error('mediaLibraryTest : getPublicDirectory failed'); + } } ``` @@ -80,75 +80,75 @@ OpenHarmony提供应用沙箱机制,增加目录可见性数据访问防线, ```ts async function copyPublic2Sandbox() { - try { - const context = getContext(this); - let media = mediaLibrary.getMediaLibrary(context); - let sandboxDirPath = context.filesDir; - let fileKeyObj = mediaLibrary.FileKey; - let fileAssetFetchOp = { - selections: fileKeyObj.DISPLAY_NAME + '= ?', - selectionArgs: ['testFile.txt'], - }; - let fetchResult = await media.getFileAssets(fileAssetFetchOp); - let fileAsset = await fetchResult.getFirstObject(); - - let fdPub = await fileAsset.open('rw'); - let fdSand = await fs.open(sandboxDirPath + '/testFile.txt', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); - await fs.copyFile(fdPub, fdSand.fd); - - await fileAsset.close(fdPub); - await fs.close(fdSand.fd); - - let content_sand = await fs.readText(sandboxDirPath + '/testFile.txt'); - console.info('content read from sandbox file: ', content_sand) - } catch (err) { - console.info('[demo] copyPublic2Sandbox fail, err: ', err); - } -} -``` - -**示例2 将应用沙箱路径文件复制到公共路径** - -```ts -async function copySandbox2Public() { + try { const context = getContext(this); let media = mediaLibrary.getMediaLibrary(context); let sandboxDirPath = context.filesDir; + let fileKeyObj = mediaLibrary.FileKey; + let fileAssetFetchOp = { + selections: fileKeyObj.DISPLAY_NAME + '= ?', + selectionArgs: ['testFile.txt'], + }; + let fetchResult = await media.getFileAssets(fileAssetFetchOp); + let fileAsset = await fetchResult.getFirstObject(); - let DIR_DOCUMENTS = mediaLibrary.DirectoryType.DIR_DOCUMENTS; - const publicDirPath = await media.getPublicDirectory(DIR_DOCUMENTS); - try { - let fileAsset = await media.createAsset(mediaLibrary.MediaType.FILE, 'testFile02.txt', publicDirPath); - console.info('createFile successfully, message = ' + fileAsset); - } catch (err) { - console.error('createFile failed, message = ' + err); - } - try { - let fileKeyObj = mediaLibrary.FileKey; - let fileAssetFetchOp = { - selections: fileKeyObj.DISPLAY_NAME + '= ?', - selectionArgs: ['testFile02.txt'], - }; - let fetchResult = await media.getFileAssets(fileAssetFetchOp); - var fileAsset = await fetchResult.getFirstObject(); - } catch (err) { - console.error('file asset get failed, message = ' + err); - } let fdPub = await fileAsset.open('rw'); - let fdSand = await fs.open(sandboxDirPath + 'testFile.txt', OpenMode.READ_WRITE); - await fs.copyFile(fdSand.fd, fdPub); + let fdSand = await fs.open(sandboxDirPath + '/testFile.txt', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); + await fs.copyFile(fdPub, fdSand.fd); + await fileAsset.close(fdPub); await fs.close(fdSand.fd); - let fdPubRead = await fileAsset.open('rw'); - try { - let arrayBuffer = new ArrayBuffer(4096); - await fs.read(fdPubRead, arrayBuffer); - var content_pub = String.fromCharCode(...new Uint8Array(arrayBuffer)); - fileAsset.close(fdPubRead); - } catch (err) { - console.error('read text failed, message = ', err); - } - console.info('content read from public file: ', content_pub); + + let content_sand = await fs.readText(sandboxDirPath + '/testFile.txt'); + console.info('content read from sandbox file: ', content_sand) + } catch (err) { + console.info('[demo] copyPublic2Sandbox fail, err: ', err); + } +} +``` + +**示例2 将应用沙箱路径文件复制到公共路径** + +```ts +async function copySandbox2Public() { + const context = getContext(this); + let media = mediaLibrary.getMediaLibrary(context); + let sandboxDirPath = context.filesDir; + + let DIR_DOCUMENTS = mediaLibrary.DirectoryType.DIR_DOCUMENTS; + const publicDirPath = await media.getPublicDirectory(DIR_DOCUMENTS); + try { + let fileAsset = await media.createAsset(mediaLibrary.MediaType.FILE, 'testFile02.txt', publicDirPath); + console.info('createFile successfully, message = ' + fileAsset); + } catch (err) { + console.error('createFile failed, message = ' + err); + } + try { + let fileKeyObj = mediaLibrary.FileKey; + let fileAssetFetchOp = { + selections: fileKeyObj.DISPLAY_NAME + '= ?', + selectionArgs: ['testFile02.txt'], + }; + let fetchResult = await media.getFileAssets(fileAssetFetchOp); + var fileAsset = await fetchResult.getFirstObject(); + } catch (err) { + console.error('file asset get failed, message = ' + err); + } + let fdPub = await fileAsset.open('rw'); + let fdSand = await fs.open(sandboxDirPath + 'testFile.txt', OpenMode.READ_WRITE); + await fs.copyFile(fdSand.fd, fdPub); + await fileAsset.close(fdPub); + await fs.close(fdSand.fd); + let fdPubRead = await fileAsset.open('rw'); + try { + let arrayBuffer = new ArrayBuffer(4096); + await fs.read(fdPubRead, arrayBuffer); + var content_pub = String.fromCharCode(...new Uint8Array(arrayBuffer)); + fileAsset.close(fdPubRead); + } catch (err) { + console.error('read text failed, message = ', err); + } + console.info('content read from public file: ', content_pub); } ``` @@ -166,20 +166,20 @@ async function copySandbox2Public() { 1. 创建用于读写示例的文件。 - ```ts - async function example() { - let mediaType = mediaLibrary.MediaType.FILE; - let DIR_DOCUMENTS = mediaLibrary.DirectoryType.DIR_DOCUMENTS; - const context = getContext(this); - let media = mediaLibrary.getMediaLibrary(context); - const path = await media.getPublicDirectory(DIR_DOCUMENTS); - media.createAsset(mediaType, "testFile.text", path).then((asset) => { - console.info("createAsset successfully:" + JSON.stringify(asset)); - }).catch((err) => { - console.error("createAsset failed with error: " + err); - }); - } - ``` +```ts +async function example() { + let mediaType = mediaLibrary.MediaType.FILE; + let DIR_DOCUMENTS = mediaLibrary.DirectoryType.DIR_DOCUMENTS; + const context = getContext(this); + let media = mediaLibrary.getMediaLibrary(context); + const path = await media.getPublicDirectory(DIR_DOCUMENTS); + media.createAsset(mediaType, "testFile.text", path).then((asset) => { + console.info("createAsset successfully:" + JSON.stringify(asset)); + }).catch((err) => { + console.error("createAsset failed with error: " + err); + }); +} +``` 2. 使用open打开文件。 @@ -195,25 +195,25 @@ async function copySandbox2Public() { ```ts async function writeOnlyPromise() { - const context = getContext(this); - let media = mediaLibrary.getMediaLibrary(context); - let fileKeyObj = mediaLibrary.FileKey; - let fileAssetFetchOp = { - selections: fileKeyObj.DISPLAY_NAME + '= ?', - selectionArgs: ['testFile.txt'], - }; - let fetchResult = await media.getFileAssets(fileAssetFetchOp); - let fileAsset = await fetchResult.getFirstObject(); - console.info('fileAssetName: ', fileAsset.displayName); - - try { - let fd = await fileAsset.open('w'); - console.info('file descriptor: ', fd); - await fs.write(fd, "Write file test content."); - await fileAsset.close(fd); - } catch (err) { - console.error('write file failed, message = ', err); - } + const context = getContext(this); + let media = mediaLibrary.getMediaLibrary(context); + let fileKeyObj = mediaLibrary.FileKey; + let fileAssetFetchOp = { + selections: fileKeyObj.DISPLAY_NAME + '= ?', + selectionArgs: ['testFile.txt'], + }; + let fetchResult = await media.getFileAssets(fileAssetFetchOp); + let fileAsset = await fetchResult.getFirstObject(); + console.info('fileAssetName: ', fileAsset.displayName); + + try { + let fd = await fileAsset.open('w'); + console.info('file descriptor: ', fd); + await fs.write(fd, "Write file test content."); + await fileAsset.close(fd); + } catch (err) { + console.error('write file failed, message = ', err); + } } ``` @@ -221,29 +221,29 @@ async function writeOnlyPromise() { ```ts async function readOnlyPromise() { - const context = getContext(this); - let media = mediaLibrary.getMediaLibrary(context); - let fileKeyObj = mediaLibrary.FileKey; - let fileAssetFetchOp = { - selections: fileKeyObj.DISPLAY_NAME + '= ?' , - selectionArgs: ['testFile.txt'], - }; - let fetchResult = await media.getFileAssets(fileAssetFetchOp); - let fileAsset = await fetchResult.getFirstObject(); - console.info('fileAssetName: ', fileAsset.displayName); - - try { - let fd = await fileAsset.open('r'); - let arrayBuffer = new ArrayBuffer(4096); - await fs.read(fd, arrayBuffer); - let fileContent = String.fromCharCode(...new Uint8Array(arrayBuffer)); - globalThis.fileContent = fileContent; - globalThis.fileName = fileAsset.displayName; - console.info('file content: ', fileContent); - await fileAsset.close(fd); - } catch (err) { - console.error('read file failed, message = ', err); - } + const context = getContext(this); + let media = mediaLibrary.getMediaLibrary(context); + let fileKeyObj = mediaLibrary.FileKey; + let fileAssetFetchOp = { + selections: fileKeyObj.DISPLAY_NAME + '= ?' , + selectionArgs: ['testFile.txt'], + }; + let fetchResult = await media.getFileAssets(fileAssetFetchOp); + let fileAsset = await fetchResult.getFirstObject(); + console.info('fileAssetName: ', fileAsset.displayName); + + try { + let fd = await fileAsset.open('r'); + let arrayBuffer = new ArrayBuffer(4096); + await fs.read(fd, arrayBuffer); + let fileContent = String.fromCharCode(...new Uint8Array(arrayBuffer)); + globalThis.fileContent = fileContent; + globalThis.fileName = fileAsset.displayName; + console.info('file content: ', fileContent); + await fileAsset.close(fd); + } catch (err) { + console.error('read file failed, message = ', err); + } } ``` diff --git a/zh-cn/application-dev/file-management/medialibrary-overview.md b/zh-cn/application-dev/file-management/medialibrary-overview.md index fd8b9fb3eceddf04b6c87948703e5879702ba5bd..5ccc5310d3304afab08d7ac98858b32a93e9bb49 100644 --- a/zh-cn/application-dev/file-management/medialibrary-overview.md +++ b/zh-cn/application-dev/file-management/medialibrary-overview.md @@ -63,65 +63,65 @@ let media = mediaLibrary.getMediaLibrary(context); 1. 在配置文件module.json5中声明权限。在配置文件的“module”标签内,增加“requestPermissions”标签,标签内容请根据实际情况填写。标签说明可参考[访问控制(权限)开发指导](../security/accesstoken-guidelines.md)。 - ```json - { - "module": { - "requestPermissions": [ - { - "name": "ohos.permission.MEDIA_LOCATION", - "reason": "$string:reason", - "usedScene": { - "abilities": [ - "EntryAbility" - ], - "when": "always" - } - }, - { - "name": "ohos.permission.READ_MEDIA", - "reason": "$string:reason", - "usedScene": { - "abilities": [ - "EntryAbility" - ], - "when": "always" - } - }, - { - "name": "ohos.permission.WRITE_MEDIA", - "reason": "$string:reason", - "usedScene": { - "abilities": [ - "EntryAbility" - ], - "when": "always" - } - } - ] - } - } - ``` +```json +{ + "module": { + "requestPermissions": [ + { + "name": "ohos.permission.MEDIA_LOCATION", + "reason": "$string:reason", + "usedScene": { + "abilities": [ + "EntryAbility" + ], + "when": "always" + } + }, + { + "name": "ohos.permission.READ_MEDIA", + "reason": "$string:reason", + "usedScene": { + "abilities": [ + "EntryAbility" + ], + "when": "always" + } + }, + { + "name": "ohos.permission.WRITE_MEDIA", + "reason": "$string:reason", + "usedScene": { + "abilities": [ + "EntryAbility" + ], + "when": "always" + } + } + ] + } +} +``` 2. 在Ability.ts中onWindowStageCreate里调用requestPermissionsFromUser进行权限校验,可以选择需要动态申请获取的权限自行添加相应代码 - ```ts - import UIAbility from '@ohos.app.ability.UIAbility'; - import abilityAccessCtrl, {Permissions} from '@ohos.abilityAccessCtrl'; - - export default class EntryAbility extends UIAbility { - onWindowStageCreate(windowStage) { - let list : Array = ['ohos.permission.READ_MEDIA', 'ohos.permission.WRITE_MEDIA']; - let permissionRequestResult; - let atManager = abilityAccessCtrl.createAtManager(); - atManager.requestPermissionsFromUser(this.context, list, (err, result) => { - if (err) { - console.error('requestPermissionsFromUserError: ' + JSON.stringify(err)); - } else { - permissionRequestResult=result; - console.info('permissionRequestResult: ' + JSON.stringify(permissionRequestResult)); - } - }); - } - } - ``` +```ts +import UIAbility from '@ohos.app.ability.UIAbility'; +import abilityAccessCtrl, {Permissions} from '@ohos.abilityAccessCtrl'; + +export default class EntryAbility extends UIAbility { + onWindowStageCreate(windowStage) { + let list : Array = ['ohos.permission.READ_MEDIA', 'ohos.permission.WRITE_MEDIA']; + let permissionRequestResult; + let atManager = abilityAccessCtrl.createAtManager(); + atManager.requestPermissionsFromUser(this.context, list, (err, result) => { + if (err) { + console.error('requestPermissionsFromUserError: ' + JSON.stringify(err)); + } else { + permissionRequestResult = result; + console.info('permissionRequestResult: ' + JSON.stringify(permissionRequestResult)); + } + }); + } +} +``` diff --git a/zh-cn/application-dev/file-management/medialibrary-resource-guidelines.md b/zh-cn/application-dev/file-management/medialibrary-resource-guidelines.md index 9277bfdf74bbd442f475e409b47325ba785f4a79..f7ff385787524cb3209c2cc3b54bc427f84ca8b2 100644 --- a/zh-cn/application-dev/file-management/medialibrary-resource-guidelines.md +++ b/zh-cn/application-dev/file-management/medialibrary-resource-guidelines.md @@ -33,27 +33,27 @@ selectionArgs: MediaType.IMAGE,媒体类型为图片。 ```ts async function example() { - let fileKeyObj = mediaLibrary.FileKey; - let fileType = mediaLibrary.MediaType.IMAGE; - let option = { - selections: fileKeyObj.MEDIA_TYPE + '= ?', - selectionArgs: [fileType.toString()], - }; - const context = getContext(this); - let media = mediaLibrary.getMediaLibrary(context); - const fetchFileResult = await media.getFileAssets(option); - fetchFileResult.getFirstObject().then((fileAsset) => { - console.log('getFirstObject.displayName : ' + fileAsset.displayName); - for (let i = 1; i < fetchFileResult.getCount(); i++) { - fetchFileResult.getNextObject().then((fileAsset) => { - console.info('fileAsset.displayName ' + i + ': ' + fileAsset.displayName); - }).catch((err) => { - console.error('Failed to get next object: ' + err); - }); - } - }).catch((err) => { - console.error('Failed to get first object: ' + err); - }); + let fileKeyObj = mediaLibrary.FileKey; + let fileType = mediaLibrary.MediaType.IMAGE; + let option = { + selections: fileKeyObj.MEDIA_TYPE + '= ?', + selectionArgs: [fileType.toString()], + }; + const context = getContext(this); + let media = mediaLibrary.getMediaLibrary(context); + const fetchFileResult = await media.getFileAssets(option); + fetchFileResult.getFirstObject().then((fileAsset) => { + console.log('getFirstObject.displayName : ' + fileAsset.displayName); + for (let i = 1; i < fetchFileResult.getCount(); i++) { + fetchFileResult.getNextObject().then((fileAsset) => { + console.info('fileAsset.displayName ' + i + ': ' + fileAsset.displayName); + }).catch((err) => { + console.error('Failed to get next object: ' + err); + }); + } + }).catch((err) => { + console.error('Failed to get first object: ' + err); + }); } ``` @@ -67,26 +67,26 @@ selectionArgs:2022-8-5,具体添加时间的字符串。 ```ts async function example() { - let fileKeyObj = mediaLibrary.FileKey; - let option = { + let fileKeyObj = mediaLibrary.FileKey; + let option = { selections: fileKeyObj.DATE_ADDED + '> ?', selectionArgs: ['2022-8-5'], - }; - const context = getContext(this); - let media = mediaLibrary.getMediaLibrary(context); - const fetchFileResult = await media.getFileAssets(option); - fetchFileResult.getFirstObject().then((fileAsset) => { - console.info('getFirstObject.displayName : ' + fileAsset.displayName); - for (let i = 1; i < fetchFileResult.getCount(); i++) { - fetchFileResult.getNextObject().then((fileAsset) => { - console.info('fileAsset.displayName ' + i + ': ' + fileAsset.displayName); - }).catch((err) => { - console.error('Failed to get next object: ' + err); - }); - } - }).catch((err) => { - console.error('Failed to get first object: ' + err); - }); + }; + const context = getContext(this); + let media = mediaLibrary.getMediaLibrary(context); + const fetchFileResult = await media.getFileAssets(option); + fetchFileResult.getFirstObject().then((fileAsset) => { + console.info('getFirstObject.displayName : ' + fileAsset.displayName); + for (let i = 1; i < fetchFileResult.getCount(); i++) { + fetchFileResult.getNextObject().then((fileAsset) => { + console.info('fileAsset.displayName ' + i + ': ' + fileAsset.displayName); + }).catch((err) => { + console.error('Failed to get next object: ' + err); + }); + } + }).catch((err) => { + console.error('Failed to get first object: ' + err); + }); } ``` @@ -98,28 +98,28 @@ order: FileKey.DATE_ADDED,根据文件添加日期排序;并设置排列 ```ts async function example() { - let fileKeyObj = mediaLibrary.FileKey; - let fileType = mediaLibrary.MediaType.IMAGE; - let option = { - selections: fileKeyObj.MEDIA_TYPE + '= ?', - selectionArgs: [fileType.toString()], - order: fileKeyObj.DATE_ADDED + " DESC", - }; - const context = getContext(this); - let media = mediaLibrary.getMediaLibrary(context); - const fetchFileResult = await media.getFileAssets(option); - fetchFileResult.getFirstObject().then((fileAsset) => { - console.info('getFirstObject.displayName : ' + fileAsset.displayName); - for (let i = 1; i < fetchFileResult.getCount(); i++) { - fetchFileResult.getNextObject().then((fileAsset) => { - console.info('fileAsset.displayName ' + i + ': ' + fileAsset.displayName); - }).catch((err) => { - console.error('Failed to get next object: ' + err); - }); - } - }).catch((err) => { - console.error('Failed to get first object: ' + err); - }); + let fileKeyObj = mediaLibrary.FileKey; + let fileType = mediaLibrary.MediaType.IMAGE; + let option = { + selections: fileKeyObj.MEDIA_TYPE + '= ?', + selectionArgs: [fileType.toString()], + order: fileKeyObj.DATE_ADDED + " DESC", + }; + const context = getContext(this); + let media = mediaLibrary.getMediaLibrary(context); + const fetchFileResult = await media.getFileAssets(option); + fetchFileResult.getFirstObject().then((fileAsset) => { + console.info('getFirstObject.displayName : ' + fileAsset.displayName); + for (let i = 1; i < fetchFileResult.getCount(); i++) { + fetchFileResult.getNextObject().then((fileAsset) => { + console.info('fileAsset.displayName ' + i + ': ' + fileAsset.displayName); + }).catch((err) => { + console.error('Failed to get next object: ' + err); + }); + } + }).catch((err) => { + console.error('Failed to get first object: ' + err); + }); } ``` @@ -133,24 +133,23 @@ selectionArgs:'myAlbum',具体相册名称。 ```ts async function example() { - let fileKeyObj = mediaLibrary.FileKey; - let fileType = mediaLibrary.MediaType.IMAGE; - let option = { - selections: fileKeyObj.ALBUM_NAME + '= ?', - selectionArgs: ['myAlbum'], - }; - const context = getContext(this); - let media = mediaLibrary.getMediaLibrary(context); - const fetchFileResult = await media.getFileAssets(option); - if (albumList.length > 0) { - fetchFileResult.getFirstObject().then((album) => { - console.info('getFirstObject.displayName : ' + album.albumName); - }).catch((err) => { - console.error('Failed to get first object: ' + err); - }); - } else { - console.info('getAlbum list is: 0'); - } + let fileKeyObj = mediaLibrary.FileKey; + let option = { + selections: fileKeyObj.ALBUM_NAME + '= ?', + selectionArgs: ['myAlbum'], + }; + const context = getContext(this); + let media = mediaLibrary.getMediaLibrary(context); + const fetchFileResult = await media.getFileAssets(option); + if (albumList.length > 0) { + fetchFileResult.getFirstObject().then((album) => { + console.info('getFirstObject.displayName : ' + album.albumName); + }).catch((err) => { + console.error('Failed to get first object: ' + err); + }); + } else { + console.info('getAlbum list is: 0'); + } } ``` @@ -169,24 +168,24 @@ async function example() { 1. 建立检索条件,用于获取目的相册实例。 - ```ts - let fileKeyObj = mediaLibrary.FileKey; - let AlbumNoArgsFetchOp = { - selections: fileKeyObj.ALBUM_NAME + '= ?', - selectionArgs: ['新建相册1'] - } - ``` +```ts +let fileKeyObj = mediaLibrary.FileKey; +let AlbumNoArgsFetchOp = { + selections: fileKeyObj.ALBUM_NAME + '= ?', + selectionArgs: ['新建相册1'] +} +``` 2. 建立检索条件,用于获取目的相册下的视频资源。 - ```ts - let fileKeyObj = mediaLibrary.FileKey; - let videoType = mediaLibrary.MediaType.VIDEO; - let videoFetchOp = { - selections: fileKeyObj.MEDIA_TYPE + '= ?', - selectionArgs: [videoType.toString()], - } - ``` +```ts +let fileKeyObj = mediaLibrary.FileKey; +let videoType = mediaLibrary.MediaType.VIDEO; +let videoFetchOp = { + selections: fileKeyObj.MEDIA_TYPE + '= ?', + selectionArgs: [videoType.toString()], +} +``` 3. 通过Album.getFileAssets获取对应的资源。 @@ -194,28 +193,28 @@ async function example() { ```ts async function getCameraImagePromise() { - const context = getContext(this); - let media = mediaLibrary.getMediaLibrary(context); - let fileKeyObj = mediaLibrary.FileKey; - let videoType = mediaLibrary.MediaType.VIDEO; - let videoFetchOp = { - selections: fileKeyObj.MEDIA_TYPE + '= ?', - selectionArgs: [videoType.toString()], - } - let AlbumNoArgsFetchOp = { - selections: fileKeyObj.ALBUM_NAME + '= ?', - selectionArgs: ['新建相册1'] - } - - let albumList = await media.getAlbums(AlbumNoArgsFetchOp); - if (albumList.length > 0) { - const album = albumList[0]; - let fetchFileResult = await album.getFileAssets(videoFetchOp); - let count = fetchFileResult.getCount(); - console.info("get mediaLibrary VIDEO number", count); - } else { - console.info('getAlbum list is: 0'); - } + const context = getContext(this); + let media = mediaLibrary.getMediaLibrary(context); + let fileKeyObj = mediaLibrary.FileKey; + let videoType = mediaLibrary.MediaType.VIDEO; + let videoFetchOp = { + selections: fileKeyObj.MEDIA_TYPE + '= ?', + selectionArgs: [videoType.toString()], + } + let AlbumNoArgsFetchOp = { + selections: fileKeyObj.ALBUM_NAME + '= ?', + selectionArgs: ['新建相册1'] + } + + let albumList = await media.getAlbums(AlbumNoArgsFetchOp); + if (albumList.length > 0) { + const album = albumList[0]; + let fetchFileResult = await album.getFileAssets(videoFetchOp); + let count = fetchFileResult.getCount(); + console.info("get mediaLibrary VIDEO number", count); + } else { + console.info('getAlbum list is: 0'); + } } ``` @@ -243,32 +242,32 @@ async function getCameraImagePromise() { ```ts async function getFirstThumbnailPromise() { - const context = getContext(this); - let media = mediaLibrary.getMediaLibrary(context); - let fileKeyObj = mediaLibrary.FileKey; - let imageType = mediaLibrary.MediaType.IMAGE; - let imagesFetchOp = { - selections: fileKeyObj.MEDIA_TYPE + '= ?', - selectionArgs: [imageType.toString()], - } - - let size = { width: 720, height: 720 }; - const fetchFileResult = await media.getFileAssets(imagesFetchOp); - if (fetchFileResult === undefined) { - console.error("get image failed with error"); - return; - } else { - const asset = await fetchFileResult.getFirstObject(); - asset.getThumbnail(size).then((pixelMap) => { - pixelMap.getImageInfo().then((info) => { - console.info('get Thumbnail info: ' + "width: " + info.size.width + " height: " + info.size.height); - }).catch((err) => { - console.error("getImageInfo failed with error: " + err); - }); - }).catch((err) => { - console.error("getImageInfo failed with error: " + err); - }); - } + const context = getContext(this); + let media = mediaLibrary.getMediaLibrary(context); + let fileKeyObj = mediaLibrary.FileKey; + let imageType = mediaLibrary.MediaType.IMAGE; + let imagesFetchOp = { + selections: fileKeyObj.MEDIA_TYPE + '= ?', + selectionArgs: [imageType.toString()], + } + + let size = { width: 720, height: 720 }; + const fetchFileResult = await media.getFileAssets(imagesFetchOp); + if (fetchFileResult === undefined) { + console.error("get image failed with error"); + return; + } else { + const asset = await fetchFileResult.getFirstObject(); + asset.getThumbnail(size).then((pixelMap) => { + pixelMap.getImageInfo().then((info) => { + console.info('get Thumbnail info: ' + "width: " + info.size.width + " height: " + info.size.height); + }).catch((err) => { + console.error("getImageInfo failed with error: " + err); + }); + }).catch((err) => { + console.error("getImageInfo failed with error: " + err); + }); + } } ``` @@ -286,16 +285,16 @@ async function getFirstThumbnailPromise() { ```ts async function example() { - let mediaType = mediaLibrary.MediaType.FILE; - let DIR_DOCUMENTS = mediaLibrary.DirectoryType.DIR_DOCUMENTS; - const context = getContext(this); - let media = mediaLibrary.getMediaLibrary(context); - const path = await media.getPublicDirectory(DIR_DOCUMENTS); - media.createAsset(mediaType, "testFile.text", path).then((asset) => { - console.info("createAsset successfully:"+ JSON.stringify(asset)); - }).catch((err) => { - console.error("createAsset failed with error: " + err); - }); + let mediaType = mediaLibrary.MediaType.FILE; + let DIR_DOCUMENTS = mediaLibrary.DirectoryType.DIR_DOCUMENTS; + const context = getContext(this); + let media = mediaLibrary.getMediaLibrary(context); + const path = await media.getPublicDirectory(DIR_DOCUMENTS); + media.createAsset(mediaType, "testFile.text", path).then((asset) => { + console.info("createAsset successfully:"+ JSON.stringify(asset)); + }).catch((err) => { + console.error("createAsset failed with error: " + err); + }); } ``` @@ -321,26 +320,26 @@ async function example() { ```ts async function example() { - let fileKeyObj = mediaLibrary.FileKey; - let fileType = mediaLibrary.MediaType.FILE; - let option = { - selections: fileKeyObj.MEDIA_TYPE + '= ?', - selectionArgs: [fileType.toString()], - }; - const context = getContext(this); - let media = mediaLibrary.getMediaLibrary(context); - const fetchFileResult = await media.getFileAssets(option); - let asset = await fetchFileResult.getFirstObject(); - if (asset === undefined) { - console.error('asset not exist'); - return; - } - //回调为空 - asset.trash(true).then(() => { - console.info("trash successfully"); - }).catch((err) => { - console.error("trash failed with error: " + err); - }); + let fileKeyObj = mediaLibrary.FileKey; + let fileType = mediaLibrary.MediaType.FILE; + let option = { + selections: fileKeyObj.MEDIA_TYPE + '= ?', + selectionArgs: [fileType.toString()], + }; + const context = getContext(this); + let media = mediaLibrary.getMediaLibrary(context); + const fetchFileResult = await media.getFileAssets(option); + let asset = await fetchFileResult.getFirstObject(); + if (asset === undefined) { + console.error('asset not exist'); + return; + } + //回调为空 + asset.trash(true).then(() => { + console.info("trash successfully"); + }).catch((err) => { + console.error("trash failed with error: " + err); + }); } ``` @@ -369,28 +368,28 @@ async function example() { ```ts async function example() { - let fileKeyObj = mediaLibrary.FileKey; - let fileType = mediaLibrary.MediaType.IMAGE; - let option = { - selections: fileKeyObj.MEDIA_TYPE + '= ?', - selectionArgs: [fileType.toString()], - }; - const context = getContext(this); - let media = mediaLibrary.getMediaLibrary(context); - const fetchFileResult = await media.getFileAssets(option); - let asset = await fetchFileResult.getFirstObject(); - if (asset === undefined) { - console.error('asset not exist'); - return; + let fileKeyObj = mediaLibrary.FileKey; + let fileType = mediaLibrary.MediaType.IMAGE; + let option = { + selections: fileKeyObj.MEDIA_TYPE + '= ?', + selectionArgs: [fileType.toString()], + }; + const context = getContext(this); + let media = mediaLibrary.getMediaLibrary(context); + const fetchFileResult = await media.getFileAssets(option); + let asset = await fetchFileResult.getFirstObject(); + if (asset === undefined) { + console.error('asset not exist'); + return; + } + asset.displayName = 'newImage.jpg'; + //回调为空 + asset.commitModify((err) => { + if (err) { + console.error('fileRename Failed '); + return; } - asset.displayName = 'newImage.jpg'; - //回调为空 - asset.commitModify((err) => { - if (err) { - console.error('fileRename Failed '); - return; - } - console.info('fileRename successful.'); - }); + console.info('fileRename successful.'); + }); } ```