diff --git a/zh-cn/application-dev/reference/apis/js-apis-fileAccess.md b/zh-cn/application-dev/reference/apis/js-apis-fileAccess.md index a8fc1059a782ea024c2865797d4bfc8d77a428c8..2106446537080299e28d36faa32cd12b99b3d273 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-fileAccess.md +++ b/zh-cn/application-dev/reference/apis/js-apis-fileAccess.md @@ -1,6 +1,6 @@ # @ohos.file.fileAccess (公共文件访问与管ç†) -fileAccess模å—是基于extension机制实现的一个对公共文件访问和æ“作的框架。该模å—一方é¢å¯¹æŽ¥å„类文件管ç†æœåŠ¡ï¼Œå¦‚媒体库ã€å¤–ç½®å˜å‚¨ç®¡ç†æœåŠ¡ç‰ï¼Œå¦ä¸€æ–¹é¢ä¸ºç³»ç»Ÿåº”用æ供一套统一的文件访问管ç†æŽ¥å£ã€‚å…¶ä¸ï¼Œåª’体库æœåŠ¡æ供本地设备ã€åˆ†å¸ƒå¼è®¾å¤‡ç‰å…¬å…±æ–‡ä»¶è®¿é—®æœåŠ¡ï¼›å¤–ç½®å˜å‚¨ç®¡ç†æœåŠ¡å¯ä»¥æ供共享盘ã€U盘ã€SDå¡ç‰è®¾å¤‡çš„公共文件访问æœåŠ¡ã€‚ +fileAccess模å—是基于extension机制实现的一个对公共文件访问和æ“作的框架。该模å—一方é¢å¯¹æŽ¥å„类文件管ç†æœåŠ¡ï¼Œå¦‚å˜å‚¨ç®¡ç†æœåŠ¡ç‰ï¼Œå¦ä¸€æ–¹é¢ä¸ºç³»ç»Ÿåº”用æ供一套统一的文件访问管ç†æŽ¥å£ã€‚å˜å‚¨ç®¡ç†æœåŠ¡å¯ä»¥ç®¡ç†å†…ç½®å˜å‚¨éƒ¨åˆ†ç›®å½•ï¼Œä»¥åŠå…±äº«ç›˜ã€U盘ã€SDå¡ç‰è®¾å¤‡ä¸Šçš„资æºã€‚ >**说明:** > @@ -124,10 +124,9 @@ createFileAccessHelper(context: Context, wants: Array<Want>) : FileAccessH createFileAccessHelper() { let fileAccessHelper = null; // wantInfos 从getFileAccessAbilityInfo()èŽ·å– - // 创建åªè¿žæŽ¥åª’体库æœåŠ¡çš„helper对象 let wantInfos = [ { - "bundleName": "com.ohos.medialibrary.medialibrarydata", + "bundleName": "com.ohos.UserFile.ExternalFileManager", "abilityName": "FileExtensionAbility", }, ] @@ -651,10 +650,10 @@ createFile(uri: string, displayName: string) : Promise<string> **示例:** ```js - // 以媒体库uri为例 + // 以内置å˜å‚¨ç›®å½•ä¸ºä¾‹ // 示例代ç sourceUri表示Download目录,该uri是对应的fileInfoä¸uri // å¼€å‘è€…åº”æ ¹æ®è‡ªå·±å®žé™…获å–çš„uriè¿›è¡Œå¼€å‘ - let sourceUri = "file://media/file/6"; + let sourceUri = "file://docs/storage/Users/currentUser/Download"; let displayName = "file1" let fileUri = null; try { @@ -695,10 +694,10 @@ createFile(uri: string, displayName: string, callback: AsyncCallback<string&g **示例:** ```js - // 以媒体库uri为例 + // 以内置å˜å‚¨ç›®å½•ä¸ºä¾‹ // 示例代ç sourceUri表示Download目录,该uri是对应的fileInfoä¸uri // å¼€å‘è€…åº”æ ¹æ®è‡ªå·±å®žé™…获å–çš„uriè¿›è¡Œå¼€å‘ - let sourceUri = "file://media/file/6"; + let sourceUri = "file://docs/storage/Users/currentUser/Download"; let displayName = "file1" try { // fileAccessHelper å‚考 fileAccess.createFileAccessHelper 示例代ç èŽ·å– @@ -744,10 +743,10 @@ mkDir(parentUri: string, displayName: string) : Promise<string> **示例:** ```js - // 以媒体库uri为例 + // 以内置å˜å‚¨ç›®å½•ä¸ºä¾‹ // 示例代ç sourceUri表示Download目录,该uri是对应的fileInfoä¸uri // å¼€å‘è€…åº”æ ¹æ®è‡ªå·±å®žé™…获å–çš„uriè¿›è¡Œå¼€å‘ - let sourceUri = "file://media/file/6"; + let sourceUri = "file://docs/storage/Users/currentUser/Download"; let dirName = "dirTest" let dirUri = null; try { @@ -788,10 +787,10 @@ mkDir(parentUri: string, displayName: string, callback: AsyncCallback<string& **示例:** ```js - // 以媒体库uri为例 + // 以内置å˜å‚¨ç›®å½•ä¸ºä¾‹ // 示例代ç sourceUri表示Download目录,该uri是对应的fileInfoä¸uri // å¼€å‘è€…åº”æ ¹æ®è‡ªå·±å®žé™…获å–çš„uriè¿›è¡Œå¼€å‘ - let sourceUri = "file://media/file/6"; + let sourceUri = "file://docs/storage/Users/currentUser/Download"; let dirName = "dirTest" try { // fileAccessHelper å‚考 fileAccess.createFileAccessHelper 示例代ç èŽ·å– @@ -837,10 +836,10 @@ openFile(uri: string, flags: OPENFLAGS) : Promise<number> **示例:** ```js - // 以媒体库uri为例 + // 以内置å˜å‚¨ç›®å½•ä¸ºä¾‹ // 示例代ç targetUri表示Download目录下文件,该uri是对应的fileInfoä¸uri // å¼€å‘è€…åº”æ ¹æ®è‡ªå·±å®žé™…获å–çš„uriè¿›è¡Œå¼€å‘ - let targetUri = "file://media/file/100"; + let targetUri = "file://docs/storage/Users/currentUser/Download/1.txt"; try { // fileAccessHelper å‚考 fileAccess.createFileAccessHelper 示例代ç èŽ·å– let fd = await fileAccessHelper.openFile(targetUri, fileAccess.OPENFLAGS.READ); @@ -874,10 +873,10 @@ openFile(uri: string, flags: OPENFLAGS, callback: AsyncCallback<number>) : **示例:** ```js - // 以媒体库uri为例 + // 以内置å˜å‚¨ç›®å½•ä¸ºä¾‹ // 示例代ç targetUri表示Download目录下文件,该uri是对应的fileInfoä¸uri // å¼€å‘è€…åº”æ ¹æ®è‡ªå·±å®žé™…获å–çš„uriè¿›è¡Œå¼€å‘ - let targetUri = "file://media/file/100"; + let targetUri = "file://docs/storage/Users/currentUser/Download/1.txt"; try { // fileAccessHelper å‚考 fileAccess.createFileAccessHelper 示例代ç èŽ·å– fileAccessHelper.openFile(targetUri, fileAccess.OPENFLAGS.READ, function (err, fd) { @@ -921,10 +920,10 @@ delete(uri: string) : Promise<number> **示例:** ```js - // 以媒体库uri为例 + // 以内置å˜å‚¨ç›®å½•ä¸ºä¾‹ // 示例代ç targetUri表示Download目录下文件,该uri是对应的fileInfoä¸uri // å¼€å‘è€…åº”æ ¹æ®è‡ªå·±å®žé™…获å–çš„uriè¿›è¡Œå¼€å‘ - let targetUri = "file://media/file/100"; + let targetUri = "file://docs/storage/Users/currentUser/Download/1.txt"; try { // fileAccessHelper å‚考 fileAccess.createFileAccessHelper 示例代ç èŽ·å– let code = await fileAccessHelper.delete(targetUri); @@ -959,10 +958,10 @@ delete(uri: string, callback: AsyncCallback<number>) : void **示例:** ```js - // 以媒体库uri为例 + // 以内置å˜å‚¨ç›®å½•ä¸ºä¾‹ // 示例代ç targetUri表示Download目录下文件,该uri是对应的fileInfoä¸uri // å¼€å‘è€…åº”æ ¹æ®è‡ªå·±å®žé™…获å–çš„uriè¿›è¡Œå¼€å‘ - let targetUri = "file://media/file/100"; + let targetUri = "file://docs/storage/Users/currentUser/Download/1.txt"; try { // fileAccessHelper å‚考 fileAccess.createFileAccessHelper 示例代ç èŽ·å– fileAccessHelper.delete(targetUri, function (err, code) { @@ -1007,11 +1006,11 @@ move(sourceFile: string, destFile: string) : Promise<string> **示例:** ```js - // 以媒体库uri为例 - // 示例代ç sourceFile destFile表示Download目录下文件或文件夹,该uri是对应的fileInfoä¸uri + // 以内置å˜å‚¨ç›®å½•ä¸ºä¾‹ + // 示例代ç sourceFile destFile表示Download目录下文件和文件夹,该uri是对应的fileInfoä¸uri // å¼€å‘è€…åº”æ ¹æ®è‡ªå·±å®žé™…获å–çš„uriè¿›è¡Œå¼€å‘ - let sourceFile = "file://media/file/102"; - let destFile = "file://media/file/101"; + let sourceFile = "file://docs/storage/Users/currentUser/Download/1.txt"; + let destFile = "file://docs/storage/Users/currentUser/Download/test"; try { // fileAccessHelper å‚考 fileAccess.createFileAccessHelper 示例代ç èŽ·å– let fileUri = await fileAccessHelper.move(sourceFile, destFile); @@ -1046,11 +1045,11 @@ move(sourceFile: string, destFile: string, callback: AsyncCallback<string> **示例:** ```js - // 以媒体库uri为例 - // 示例代ç sourceFile destFile表示Download目录下文件或文件夹,该uri是对应的fileInfoä¸uri + // 以内置å˜å‚¨ç›®å½•ä¸ºä¾‹ + // 示例代ç sourceFile destFile表示Download目录下文件和文件夹,该uri是对应的fileInfoä¸uri // å¼€å‘è€…åº”æ ¹æ®è‡ªå·±å®žé™…获å–çš„uriè¿›è¡Œå¼€å‘ - let sourceFile = "file://media/file/102"; - let destFile = "file://media/file/101"; + let sourceFile = "file://docs/storage/Users/currentUser/Download/1.txt"; + let destFile = "file://docs/storage/Users/currentUser/Download/test"; try { // fileAccessHelper å‚考 fileAccess.createFileAccessHelper 示例代ç èŽ·å– fileAccessHelper.move(sourceFile, destFile, function (err, fileUri) { @@ -1095,10 +1094,10 @@ rename(uri: string, displayName: string) : Promise<string> **示例:** ```js - // 以媒体库uri为例 + // 以内置å˜å‚¨ç›®å½•ä¸ºä¾‹ // 示例代ç sourceDir表示Download目录下文件,该uri是对应的fileInfoä¸uri // å¼€å‘è€…åº”æ ¹æ®è‡ªå·±å®žé™…获å–çš„uriè¿›è¡Œå¼€å‘ - let sourceDir = "file://media/file/100"; + let sourceDir = "file://docs/storage/Users/currentUser/Download/1.txt"; try { // fileAccessHelper å‚考 fileAccess.createFileAccessHelper 示例代ç èŽ·å– let DestDir = await fileAccessHelper.rename(sourceDir, "testDir"); @@ -1133,10 +1132,10 @@ rename(uri: string, displayName: string, callback: AsyncCallback<string>) **示例:** ```js - // 以媒体库uri为例 + // 以内置å˜å‚¨ç›®å½•ä¸ºä¾‹ // 示例代ç sourceDir表示Download目录下文件,该uri是对应的fileInfoä¸uri // å¼€å‘è€…åº”æ ¹æ®è‡ªå·±å®žé™…获å–çš„uriè¿›è¡Œå¼€å‘ - let sourceDir = "file://media/file/100"; + let sourceDir = "file://docs/storage/Users/currentUser/Download/1.txt"; try { // fileAccessHelper å‚考 fileAccess.createFileAccessHelper 示例代ç èŽ·å– fileAccessHelper.rename(sourceDir, "testDir", function (err, DestDir) { @@ -1180,10 +1179,10 @@ access(sourceFileUri: string) : Promise<boolean> **示例:** ```js - // 以媒体库uri为例 + // 以内置å˜å‚¨ç›®å½•ä¸ºä¾‹ // 示例代ç sourceDir表示Download目录下文件,该uri是对应的fileInfoä¸uri // å¼€å‘è€…åº”æ ¹æ®è‡ªå·±å®žé™…获å–çš„uriè¿›è¡Œå¼€å‘ - let sourceDir = "file://media/file/100"; + let sourceDir = "file://docs/storage/Users/currentUser/Download/1.txt"; try { // fileAccessHelper å‚考 fileAccess.createFileAccessHelper 示例代ç èŽ·å– let existJudgment = await fileAccessHelper.access(sourceDir); @@ -1220,10 +1219,10 @@ access(sourceFileUri: string, callback: AsyncCallback<boolean>) : void **示例:** ```js - // 以媒体库uri为例 - // 示例代ç sourceDir表示Download目录下文件,该uri是对应的fileInfoä¸uri + // 以内置å˜å‚¨ç›®å½•ä¸ºä¾‹ + // 示例代ç sourceDir表示Download目录下文件夹,该uri是对应的fileInfoä¸uri // å¼€å‘è€…åº”æ ¹æ®è‡ªå·±å®žé™…获å–çš„uriè¿›è¡Œå¼€å‘ - let sourceDir = "file://media/file/100"; + let sourceDir = "file://docs/storage/Users/currentUser/Download/test"; try { // fileAccessHelper å‚考 fileAccess.createFileAccessHelper 示例代ç èŽ·å– fileAccessHelper.access(sourceDir, function (err, existJudgment) { @@ -1266,10 +1265,10 @@ getFileInfoFromUri(uri: string) : Promise\<FileInfo> **示例:** ```js - // 以媒体库uri为例 + // 以内置å˜å‚¨ç›®å½•ä¸ºä¾‹ // 示例代ç sourceUri表示Download目录,该uri是对应的fileInfoä¸uri // å¼€å‘è€…åº”æ ¹æ®è‡ªå·±å®žé™…获å–çš„uriè¿›è¡Œå¼€å‘ - let sourceUri = "file://media/file/6"; + let sourceUri = "file://docs/storage/Users/currentUser/Download"; try { // fileAccessHelper å‚考 fileAccess.createFileAccessHelper 示例代ç èŽ·å– let fileInfo = await fileAccessHelper.getFileInfoFromUri(sourceUri); @@ -1298,10 +1297,10 @@ getFileInfoFromUri(uri: string, callback: AsyncCallback\<FileInfo>) : void **示例:** ```js - // 以媒体库uri为例 + // 以内置å˜å‚¨ç›®å½•ä¸ºä¾‹ // 示例代ç sourceUri表示Download目录,该uri是对应的fileInfoä¸uri // å¼€å‘è€…åº”æ ¹æ®è‡ªå·±å®žé™…获å–çš„uriè¿›è¡Œå¼€å‘ - let sourceUri = "file://media/file/6"; + let sourceUri = "file://docs/storage/Users/currentUser/Download"; try { // fileAccessHelper å‚考 fileAccess.createFileAccessHelper 示例代ç èŽ·å– fileAccessHelper.getFileInfoFromUri(sourceUri, function (err, fileInfo) { @@ -1342,7 +1341,6 @@ getFileInfoFromRelativePath(relativePath: string) : Promise\<FileInfo> **示例:** ```js - // 以媒体库relativePath为例 // 示例代ç relativePath表示Download目录,该relativePath是对应的fileInfoä¸relativePath // å¼€å‘è€…åº”æ ¹æ®è‡ªå·±å®žé™…获å–çš„relativePathè¿›è¡Œå¼€å‘ let relativePath = "Download/"; @@ -1374,7 +1372,6 @@ getFileInfoFromRelativePath(relativePath: string, callback: AsyncCallback\<FileI **示例:** ```js - // 以媒体库relativePath为例 // 示例代ç relativePath表示Download目录,该relativePath是对应的fileInfoä¸relativePath // å¼€å‘è€…åº”æ ¹æ®è‡ªå·±å®žé™…获å–çš„relativePathè¿›è¡Œå¼€å‘ let relativePath = "Download/"; @@ -1418,7 +1415,7 @@ query(uri:string, metaJson: string) : Promise<string> **示例:** ```js -var imageFileRelativePath = "Download/queryTest/image/01.jpg"; +var imageFileRelativePath = "/storage/Users/currentUser/Download/queryTest/image/01.jpg"; var jsonStrSingleRelativepath = JSON.stringify({ [fileAccess.FileKey.RELATIVE_PATH]: "" }); try { // fileAccessHelper å‚考 fileAccess.createFileAccessHelper 示例代ç èŽ·å– @@ -1451,7 +1448,7 @@ query(uri:string, metaJson: string, callback: AsyncCallback<string>) : voi **示例:** ```js -var imageFileRelativePath = "Download/queryTest/image/01.jpg"; +var imageFileRelativePath = "/storage/Users/currentUser/Download/queryTest/image/01.jpg"; var jsonStrSingleRelativepath = JSON.stringify({ [fileAccess.FileKey.RELATIVE_PATH]: "" }); try { // fileAccessHelper å‚考 fileAccess.createFileAccessHelper 示例代ç èŽ·å– @@ -1482,8 +1479,8 @@ copy(sourceUri: string, destUri: string, force?: boolean) : Promise<Array< | å‚æ•°å | 类型 | å¿…å¡« | 说明 | | --------- | ------- | ---- | ------------------------------------------------------------ | -| sourceUri | string | 是 | å¾…æ‹·è´çš„æºæ–‡ä»¶(夹)çš„ uri,例如:file://media/file/102 | -| destUri | string | 是 | ç›®æ ‡æ–‡ä»¶å¤¹çš„ uri,例如:file://media/file/101 | +| sourceUri | string | 是 | å¾…æ‹·è´çš„æºæ–‡ä»¶(夹)çš„ uri,例如:file://docs/storage/Users/currentUser/Download/1.txt | +| destUri | string | 是 | ç›®æ ‡æ–‡ä»¶å¤¹çš„ uri,例如:file://docs/storage/Users/currentUser/Download/test | | force | boolean | å¦ | å«æœ‰åŒå文件时是å¦å¼ºåˆ¶è¦†ç›–文件,force 为 true 时强制覆盖文件,force 为空或 false æ—¶ä¸å¼ºåˆ¶è¦†ç›–文件 | **返回值:** @@ -1495,11 +1492,11 @@ copy(sourceUri: string, destUri: string, force?: boolean) : Promise<Array< **示例 1:force 为空** ```js -// 以媒体库uri为例 +// 以内置å˜å‚¨ç›®å½•ä¸ºä¾‹ // 示例代ç ä¸çš„sourceFile表示Download目录下的æºæ–‡ä»¶(夹),destFile表示Downloadç›®å½•ä¸‹çš„ç›®æ ‡æ–‡ä»¶å¤¹ï¼Œè¯¥uri对应fileInfoä¸çš„uri // å¼€å‘è€…åº”æ ¹æ®è‡ªå·±å®žé™…获å–çš„uriè¿›è¡Œå¼€å‘ -let sourceFile = "file://media/file/102"; -let destFile = "file://media/file/101"; +let sourceFile = "file://docs/storage/Users/currentUser/Download/1.txt"; +let destFile = "file://docs/storage/Users/currentUser/Download/test"; try { // fileAccessHelper å‚考 fileAccess.createFileAccessHelper 示例代ç èŽ·å– let copyResult = await fileAccessHelper.copy(sourceFile, destFile); @@ -1521,11 +1518,11 @@ try { **示例 2:force 为 true** ```js -// 以媒体库uri为例 +// 以内置å˜å‚¨ç›®å½•ä¸ºä¾‹ // 示例代ç ä¸çš„sourceFile表示Download目录下的æºæ–‡ä»¶(夹),destFile表示Downloadç›®å½•ä¸‹çš„ç›®æ ‡æ–‡ä»¶å¤¹ï¼Œè¯¥uri对应fileInfoä¸çš„uri // å¼€å‘è€…åº”æ ¹æ®è‡ªå·±å®žé™…获å–çš„uriè¿›è¡Œå¼€å‘ -let sourceFile = "file://media/file/102"; -let destFile = "file://media/file/101"; +let sourceFile = "file://docs/storage/Users/currentUser/Download/1.txt"; +let destFile = "file://docs/storage/Users/currentUser/Download/test"; try { // fileAccessHelper å‚考 fileAccess.createFileAccessHelper 示例代ç èŽ·å– let copyResult = await fileAccessHelper.copy(sourceFile, destFile, true); @@ -1558,18 +1555,18 @@ copy(sourceUri: string, destUri: string, callback: AsyncCallback<Array<Cop | å‚æ•°å | 类型 | å¿…å¡« | 说明 | | --------- | ------------------------------------------------ | ---- | ------------------------------------------------------------ | -| sourceUri | string | 是 | å¾…æ‹·è´çš„æºæ–‡ä»¶(夹)çš„ uri,例如:file://media/file/102 | -| destUri | string | 是 | ç›®æ ‡æ–‡ä»¶å¤¹çš„ uri,例如:file://media/file/101 | +| sourceUri | string | 是 | å¾…æ‹·è´çš„æºæ–‡ä»¶(夹)çš„ uri,例如:file://docs/storage/Users/currentUser/Download/1.txt | +| destUri | string | 是 | ç›®æ ‡æ–‡ä»¶å¤¹çš„ uri,例如:file://docs/storage/Users/currentUser/Download/test | | callback | AsyncCallback<Array<[CopyResult](#copyresult10)>> | 是 | 返回 copyresult 数组,copyResult 为å¤åˆ¶æ“作失败的返回信æ¯ï¼Œå¤åˆ¶æˆåŠŸæ— è¿”å›žä¿¡æ¯ | **示例:** ```js -// 以媒体库uri为例 +// 以内置å˜å‚¨ç›®å½•ä¸ºä¾‹ // 示例代ç ä¸çš„sourceFile表示Download目录下的æºæ–‡ä»¶(夹),destFile表示Downloadç›®å½•ä¸‹çš„ç›®æ ‡æ–‡ä»¶å¤¹ï¼Œè¯¥uri对应fileInfoä¸çš„uri // å¼€å‘è€…åº”æ ¹æ®è‡ªå·±å®žé™…获å–çš„uriè¿›è¡Œå¼€å‘ -let sourceFile = "file://media/file/102"; -let destFile = "file://media/file/101"; +let sourceFile = "file://docs/storage/Users/currentUser/Download/1.txt"; +let destFile = "file://docs/storage/Users/currentUser/Download/test"; try { // fileAccessHelper å‚考 fileAccess.createFileAccessHelper 示例代ç èŽ·å– fileAccessHelper.copy(sourceFile, destFile, async (err, copyResult) => { @@ -1607,19 +1604,19 @@ copy(sourceUri: string, destUri: string, force: boolean, callback: AsyncCallback | å‚æ•°å | 类型 | å¿…å¡« | 说明 | | --------- | ------------------------------------------------ | ---- | ------------------------------------------------------------ | -| sourceUri | string | 是 | å¾…æ‹·è´çš„æºæ–‡ä»¶(夹)çš„ uri,例如:file://media/file/102 | -| destUri | string | 是 | ç›®æ ‡æ–‡ä»¶å¤¹çš„ uri,例如:file://media/file/101 | +| sourceUri | string | 是 | å¾…æ‹·è´çš„æºæ–‡ä»¶(夹)çš„ uri,例如:file://docs/storage/Users/currentUser/Download/1.txt | +| destUri | string | 是 | ç›®æ ‡æ–‡ä»¶å¤¹çš„ uri,例如:file://docs/storage/Users/currentUser/Download/test | | force | boolean | 是 | å«æœ‰åŒå文件时是å¦å¼ºåˆ¶è¦†ç›–文件,force 为 true 时强制覆盖文件,force 为空或 false æ—¶ä¸å¼ºåˆ¶è¦†ç›–文件 | | callback | AsyncCallback<Array<[CopyResult](#copyresult10)>> | 是 | 返回 copyresult 数组,copyResult 为å¤åˆ¶æ“作失败的返回信æ¯ï¼Œå¤åˆ¶æˆåŠŸæ— è¿”å›žä¿¡æ¯ | **示例:** ```js -// 以媒体库uri为例 +// 以内置å˜å‚¨ç›®å½•ä¸ºä¾‹ // 示例代ç ä¸çš„sourceFile表示Download目录下的æºæ–‡ä»¶(夹),destFile表示Downloadç›®å½•ä¸‹çš„ç›®æ ‡æ–‡ä»¶å¤¹ï¼Œè¯¥uri对应fileInfoä¸çš„uri // å¼€å‘è€…åº”æ ¹æ®è‡ªå·±å®žé™…获å–çš„uriè¿›è¡Œå¼€å‘ -let sourceFile = "file://media/file/102"; -let destFile = "file://media/file/101"; +let sourceFile = "file://docs/storage/Users/currentUser/Download/1.txt"; +let destFile = "file://docs/storage/Users/currentUser/Download/test"; try { // fileAccessHelper å‚考 fileAccess.createFileAccessHelper 示例代ç èŽ·å– fileAccessHelper.copy(sourceFile, destFile, true, async (err, copyResult) => { @@ -1643,6 +1640,249 @@ try { } ``` +### registerObserver<sup>10+</sup> + +registerObserver(uri: string, notifyForDescendants: boolean, callback: Callback<NotifyMessage>): void + +注册指定uriçš„callback。uri与callbackå¯ä»¥ä¸ºå¤šå¯¹å¤šçš„关系,推è使用一个callback监å¬ä¸€ä¸ªuri。 + +**系统能力**:SystemCapability.FileManagement.UserFileService + +**需è¦æƒé™**:ohos.permission.FILE_ACCESS_MANAGER + +**å‚数:** + +| å‚æ•°å | 类型 | å¿…å¡« | 说明 | +| -------------------- | ------------------------------------------------- | ---- | ------------------------------ | +| uri | string | 是 | 文件或目录的uri | +| notifyForDescendants | boolean | 是 | 监å¬ç›®å½•æ—¶ï¼Œæ˜¯å¦ç›‘å¬å文件å˜åŒ– | +| callback | Callback<[NotifyMessage](#notifymessage10)> | 是 | è¿”å›žé€šçŸ¥ä¿¡æ¯ | + +**示例1:注册一个callback对一个uri的监å¬** + +```js +let DirUri = 'file://docs/storage/Users/currentUser/Documents'; +try { + // fileAccessHelper å‚考 fileAccess.createFileAccessHelper 示例代ç èŽ·å– + let dirUri1 = await fileAccessHelper.mkDir(DirUri, 'NOTIFY_DIR1'); + let dirUri2 = await fileAccessHelper.mkDir(DirUri, 'NOTIFY_DIR2'); + // å› æ³¨å†Œæ—¶notifyForDescendantså‚数为true所以期待收到两次通知 + // uri为'file://docs/storage/Users/currentUser/Documents/NOTIFY_DIR1/SUB_FILE',事件类型为NOTIFY_MOVED_FROM + // uri为'file://docs/storage/Users/currentUser/Documents/NOTIFY_DIR1/SUB_FILE',事件类型为NOTIFY_MOVE_SELF + const callbackDir1 = (NotifyMessageDir) => { + if (NotifyMessageDir != undefined) { + console.log('NotifyType: ' + NotifyMessageDir.NotifyType + 'NotifyUri:' + NotifyMessageDir.uri[0]); + } else { + console.error("NotifyMessageDir is undefined"); + } + } + // 期待收到uri为'file://docs/storage/Users/currentUser/Documents/NOTIFY_DIR2/SUB_FILE',事件类型为NOTIFY_MOVED_TO + const callbackDir2 = (NotifyMessageDir) => { + if (NotifyMessageDir != undefined) { + console.log('NotifyType: ' + NotifyMessageDir.NotifyType + 'NotifyUri:' + NotifyMessageDir.uri[0]); + } else { + console.error("NotifyMessageDir is undefined"); + } + } + // 期待收到uri为'file://docs/storage/Users/currentUser/Documents/NOTIFY_DIR1/SUB_FILE',事件类型为NOTIFY_MOVE_SELF + // 期待收到uri为'file://docs/storage/Users/currentUser/Documents/NOTIFY_DIR1/SUB_FILE',事件类型为NOTIFY_MOVED_FROM + const callbackFile = (NotifyMessageDir) => { + if (NotifyMessageDir != undefined) { + console.log('NotifyType: ' + NotifyMessageDir.NotifyType + 'NotifyUri:' + NotifyMessageDir.uri[0]); + } else { + console.error("NotifyMessageDir is undefined"); + } + } + let fileUri = await.fileAccessHelper.createFile(dirUri1, 'SUB_FILE'); + fileAccessHelper.registerObserver(dirUri1, true, callbackDir1); + fileAccessHelper.registerObserver(dirUri2, true, callbackDir2); + // 如果ä¸ç›‘å¬è¢«ç§»åŠ¨æ–‡ä»¶æœ¬èº«ï¼Œå°†ä¸ä¼šè§¦å‘NOTIFY_MOVE_SELF事件 + fileAccessHelper.registerObserver(fileUri, true, callbackFile); + let moveFileUri = await fileAccessHelper.move(fileUri, dirUri2); + // 注册完ä¸åº”ç«‹å³è§£æ³¨å†Œï¼Œå¯èƒ½å˜åœ¨è§£æ³¨å†Œå…ˆäºŽé€šçŸ¥è¿”回的情景,这将收ä¸åˆ°é€šçŸ¥äº‹ä»¶ + fileAccessHelper.unregisterObserver(dirUri1, callbackDir1); + fileAccessHelper.unregisterObserver(dirUri2, callbackDir2); + fileAccessHelper.unregisterObserver(fileUri, callbackFile); +} catch (error) { + console.error("registerObserver failed, errCode:" + error.code + ", errMessage:" + error.message); +} +``` + +**示例2:使用相åŒuriã€notifyForDescendantsã€callbacké‡å¤æ³¨å†Œ** + +```js +let DirUri = 'file://docs/storage/Users/currentUser/Documents'; +try { + // fileAccessHelper å‚考 fileAccess.createFileAccessHelper 示例代ç èŽ·å– + let dirUri = await fileAccessHelper.mkDir(DirUri, 'NOTIFY_DIR'); + // 期待收到uri为'file://docs/storage/Users/currentUser/Documents/NOTIFY_DIR/SUB_DIR',事件类型为NOTIFY_ADD + const callbackDir = (NotifyMessageDir) => { + if (NotifyMessageDir != undefined) { + console.log('NotifyType: ' + NotifyMessageDir.NotifyType + 'NotifyUri:' + NotifyMessageDir.uri[0]); + } else { + console.error("NotifyMessageDir is undefined"); + } + } + fileAccessHelper.registerObserver(dirUri, true, callbackDir); + // 返回注册æˆåŠŸï¼Œä»…在logä¸æ示é‡å¤æ³¨å†Œ + fileAccessHelper.registerObserver(dirUri, true, callbackDir); + let subDirUri = await fileAccessHelper.mkDir(dirUri, 'SUB_DIR'); + // 注册完ä¸åº”ç«‹å³è§£æ³¨å†Œï¼Œå¯èƒ½å˜åœ¨è§£æ³¨å†Œå…ˆäºŽé€šçŸ¥è¿”回的情景,这将收ä¸åˆ°é€šçŸ¥äº‹ä»¶ + fileAccessHelper.unregisterObserver(dirUri, callbackDir); +} catch (error) { + console.error("registerObserver failed, errCode:" + error.code + ", errMessage:" + error.message); +} +``` + +**示例3:使用相åŒuriã€callbackåŠä¸åŒnotifyForDescendantsé‡å¤æ³¨å†Œä¼šé‡ç½®notifyForDescendants** + +```js +let DirUri = 'file://docs/storage/Users/currentUser/Documents'; +try { + // fileAccessHelper å‚考 fileAccess.createFileAccessHelper 示例代ç èŽ·å– + let dirUri = await fileAccessHelper.mkDir(DirUri, 'NOTIFY_DIR'); + // 期待第一次收到uri为'file://docs/storage/Users/currentUser/Documents/NOTIFY_DIR/SUB_FILE_1',事件类型为NOTIFY_ADD + // æœŸå¾…æ— ç¬¬äºŒæ¬¡è¿”å›ž + const callbackDir = (NotifyMessageDir) => { + if (NotifyMessageDir != undefined) { + console.log('NotifyType: ' + NotifyMessageDir.NotifyType + 'NotifyUri:' + NotifyMessageDir.uri[0]); + } else { + console.error("NotifyMessageDir is undefined"); + } + } + fileAccessHelper.registerObserver(dirUri, true, callbackDir); + let subFile1 = await fileAccessHelper.createFile(dirUri, 'SUB_FILE_1'); + // 注册æˆåŠŸä¿®æ”¹notifyForDescendants为false,ä¸æ„ŸçŸ¥å文件的å˜åŒ– + fileAccessHelper.registerObserver(dirUri, false, callbackDir); + let subFile2 = await fileAccessHelper.createFile(dirUri, 'SUB_FILE_2'); + // 注册完ä¸åº”ç«‹å³è§£æ³¨å†Œï¼Œå¯èƒ½å˜åœ¨è§£æ³¨å†Œå…ˆäºŽé€šçŸ¥è¿”回的情景,这将收ä¸åˆ°é€šçŸ¥äº‹ä»¶ + fileAccessHelper.unregisterObserver(dirUri, callbackDir); +} catch (error) { + console.error("registerObserver failed, errCode:" + error.code + ", errMessage:" + error.message); +} +``` + +### unregisterObserver<sup>10+</sup> + + unregisterObserver(uri: string, callback: Callback<NotifyMessage>): void + +å–消注册指定的uriå’Œcallback。 + +**系统能力**:SystemCapability.FileManagement.UserFileService + +**需è¦æƒé™**:ohos.permission.FILE_ACCESS_MANAGER + +**å‚数:** + +| å‚æ•°å | 类型 | å¿…å¡« | 说明 | +| -------- | ------------------------------------------------- | ---- | ------------------------- | +| uri | string | 是 | 文件或目录的uri | +| callback | Callback<[NotifyMessage](#notifymessage10)> | 是 | 解注册uri下对应的callback | + +**示例1: 解注册监å¬uri的一个callback** + +```js +let DirUri = 'file://docs/storage/Users/currentUser/Documents'; +try { + // fileAccessHelper å‚考 fileAccess.createFileAccessHelper 示例代ç èŽ·å– + let dirUri = await fileAccessHelper.mkDir(DirUri, 'NOTIFY_DIR'); + // 期待收到uri为'file://docs/storage/Users/currentUser/Documents/NOTIFY_DIR',事件类型为NOTIFY_DELETE + const callbackDir = (NotifyMessageDir) => { + if (NotifyMessageDir != undefined) { + console.log('NotifyType: ' + NotifyMessageDir.NotifyType + 'NotifyUri:' + NotifyMessageDir.uri[0]); + } else { + console.error("NotifyMessageDir is undefined"); + } + } + fileAccessHelper.registerObserver(dirUri, true, callbackDir); + await fileAccessHelper.delete(dirUri); + // 注册完ä¸åº”ç«‹å³è§£æ³¨å†Œï¼Œå¯èƒ½å˜åœ¨è§£æ³¨å†Œå…ˆäºŽé€šçŸ¥è¿”回的情景,这将收ä¸åˆ°é€šçŸ¥äº‹ä»¶ + fileAccessHelper.unregisterObserver(dirUri, callbackDir); +} catch (error) { + console.error("unregisterObserver failed, errCode:" + error.code + ", errMessage:" + error.message); +} +``` + +**示例2:对监å¬uriçš„æŸä¸€callbacké‡å¤è§£æ³¨å†Œ** + +```js +let DirUri = 'file://docs/storage/Users/currentUser/Documents'; +try { + // fileAccessHelper å‚考 fileAccess.createFileAccessHelper 示例代ç èŽ·å– + let dirUri = await fileAccessHelper.mkDir(DirUri, 'NOTIFY_DIR'); + // 期待收到uri为'file://docs/storage/Users/currentUser/Documents/NOTIFY_DIR',事件类型为NOTIFY_DELETE + const callbackDir = (NotifyMessageDir) => { + if (NotifyMessageDir != undefined) { + console.log('NotifyType: ' + NotifyMessageDir.NotifyType + 'NotifyUri:' + NotifyMessageDir.uri[0]); + } else { + console.error("NotifyMessageDir is undefined"); + } + } + fileAccessHelper.registerObserver(dirUri, true, callbackDir); + await fileAccessHelper.delete(dirUri); + // 注册完ä¸åº”ç«‹å³è§£æ³¨å†Œï¼Œå¯èƒ½å˜åœ¨è§£æ³¨å†Œå…ˆäºŽé€šçŸ¥è¿”回的情景,这将收ä¸åˆ°é€šçŸ¥äº‹ä»¶ + fileAccessHelper.unregisterObserver(dirUri, callbackDir); + // 解注册失败,抛出错误ç E_CAN_NOT_FIND_URI + fileAccessHelper.unregisterObserver(dirUri, callbackDir); +} catch (error) { + console.error("unregisterObserver failed, errCode:" + error.code + ", errMessage:" + error.message); +} +``` + +### unregisterObserver<sup>10+</sup> + + unregisterObserver(uri: string): void + +å–消注册指定的uri对应的所有callback。 + +**系统能力**:SystemCapability.FileManagement.UserFileService + +**需è¦æƒé™**:ohos.permission.FILE_ACCESS_MANAGER + +**å‚数:** + +| å‚æ•°å | 类型 | å¿…å¡« | 说明 | +| ------ | ------ | ---- | --------------- | +| uri | string | 是 | 文件或目录的uri | + +**示例1:解注册监å¬uri的所有callback** + +```js +let DirUri = 'file://docs/storage/Users/currentUser/Documents'; +try { + // fileAccessHelper å‚考 fileAccess.createFileAccessHelper 示例代ç èŽ·å– + let dirUri = await fileAccessHelper.mkDir(DirUri, 'NOTIFY_DIR'); + // 期待收到uri为'file://docs/storage/Users/currentUser/Documents/NOTIFY_DIR/SUB_FILE',事件类型为NOTIFY_MOVED_FROM + // 期待收到uri为'file://docs/storage/Users/currentUser/Documents/NOTIFY_DIR/RENAME_FILE',事件类型为NOTIFY_MOVED_TO + const callbackDir1 = (NotifyMessageDir) => { + if (NotifyMessageDir != undefined) { + console.log('NotifyType: ' + NotifyMessageDir.NotifyType + 'NotifyUri:' + NotifyMessageDir.uri[0]); + } else { + console.error("NotifyMessageDir is undefined"); + } + } + // 期待收ä¸åˆ°ä»»ä½•äº‹ä»¶ + const callbackDir2 = (NotifyMessageDir) => { + if (NotifyMessageDir != undefined) { + console.log('NotifyType: ' + NotifyMessageDir.NotifyType + 'NotifyUri:' + NotifyMessageDir.uri[0]); + } else { + console.error("NotifyMessageDir is undefined"); + } + } + let fileUri = await fileAccessHelper.createFile(dirUri, 'SUB_FILE'); + fileAccessHelper.registerObserver(dirUri, true, callbackDir1); + // æ¤å¤„注册为ä¸å…³æ³¨ä¸‹ä¸€çº§ï¼Œå°†æ”¶ä¸åˆ°å…³äºŽä¸‹ä¸€çº§çš„事件 + fileAccessHelper.registerObserver(dirUri, false, callbackDir2); + let renameUri = await fileAccessHelper.rename(fileUri, 'RENAME_FILE'); + // å–消注册监å¬dirUri的所有callback(callbackDir1ã€callbackDir2) + // 注册完ä¸åº”ç«‹å³è§£æ³¨å†Œï¼Œå¯èƒ½å˜åœ¨è§£æ³¨å†Œå…ˆäºŽé€šçŸ¥è¿”回的情景,这将收ä¸åˆ°é€šçŸ¥äº‹ä»¶ + fileAccessHelper.unregisterObserver(dirUri); + await fileAccessHelper.delete(dirUri); +} catch (error) { + console.error("unregisterObserver failed, errCode:" + error.code + ", errMessage:" + error.message); +} +``` + ## CopyResult<sup>10+</sup> 表示å¤åˆ¶æ“作失败时的返回信æ¯ï¼Œå¤åˆ¶æˆåŠŸæ—¶åˆ™æ²¡æœ‰è¿”回信æ¯ã€‚ @@ -1687,3 +1927,36 @@ try { | DATE_MODIFIED | 'date_modified' | 文件的修改日期,例如1665310670 | | RELATIVE_PATH | 'relative_path' | 相对路径,例如Pictures/Screenshots/ | | FILE_SIZE | 'size' | 文件(夹)大å°ï¼ˆå•ä½ï¼šå—节) | + +## NotifyType<sup>10+</sup> + +枚举,通知类型。 + +**模型约æŸ**:æ¤æŽ¥å£ä»…å¯åœ¨Stage模型下使用。 + +**系统能力**:SystemCapability.FileManagement.UserFileService + +**需è¦æƒé™**:ohos.permission.FILE_ACCESS_MANAGER + +| å称 | 值 | 说明 | +| ----------------- | ---- | ------------------------------------------------------------ | +| NOTIFY_ADD | 0 | 表示新增文件(详è§registerObserver接å£çš„示例2ã€ç¤ºä¾‹3) | +| NOTIFY_DELETE | 1 | è¡¨ç¤ºåˆ é™¤æ–‡ä»¶ï¼ˆè¯¦è§unregisterObserver(uri: string, callback: Callback<NotifyMessage>)接å£çš„示例1ã€ç¤ºä¾‹2) | +| NOTIFY_MOVED_TO | 2 | 表示移动至该文件(对目录下å文件或目录执行renameæ“作,或外部文件或目录执行moveæ“作到本文件,详è§registerObserver接å£çš„示例1,åŠunregisterObserver(uri: string)接å£çš„示例1)| +| NOTIFY_MOVED_FROM | 3 | 表示自该文件移出(对目录下å文件或目录执行renameæ“作,或å文件(夹)执行moveæ“作从该文件夹内移出,详è§registerObserver接å£çš„示例1,åŠunregisterObserver(uri: string)接å£çš„示例1) | +| NOTIFY_MOVE_SELF | 4 | 表示本文件被移动(如对文件或文件夹执行rename或moveæ“作时,监å¬è¯¥æ–‡ä»¶ï¼ˆå¤¹ï¼‰çš„callback收到该事件,详è§registerObserver接å£çš„示例1) | + +## NotifyMessage<sup>10+</sup> + +通知回调函数的值。 + +**模型约æŸ**:æ¤æŽ¥å£ä»…å¯åœ¨Stage模型下使用。 + +**系统能力**:SystemCapability.FileManagement.UserFileService + +**需è¦æƒé™**:ohos.permission.FILE_ACCESS_MANAGER + +| å称 | 类型 | å¯è¯» | å¯å†™ | 说明 | +| ---- | --------------------------- | ---- | ---- | --------------------------------------------------------- | +| type | [NotifyType](#notifytype10) | 是 | å¦ | å˜æ›´çš„通知类型 | +| uris | Array<string> | 是 | å¦ | 所å˜æ›´æ–‡ä»¶çš„uri集åˆï¼Œç›®å‰ä»…支æŒå•æ¡é€šçŸ¥ï¼ŒåŽåºæ”¯æŒå¤šæ¡é€šçŸ¥ |