Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Docs
提交
911a4683
D
Docs
项目概览
OpenHarmony
/
Docs
大约 1 年 前同步成功
通知
159
Star
292
Fork
28
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
Docs
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
911a4683
编写于
2月 06, 2023
作者:
O
openharmony_ci
提交者:
Gitee
2月 06, 2023
浏览文件
操作
浏览文件
下载
差异文件
!14231 file-management log abarbeitung
Merge pull request !14231 from 胡伟奇/file-management
上级
4e92f598
5a7f812b
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
151 addition
and
136 deletion
+151
-136
zh-cn/application-dev/file-management/medialibrary-album-guidelines.md
...tion-dev/file-management/medialibrary-album-guidelines.md
+6
-6
zh-cn/application-dev/file-management/medialibrary-filepath-guidelines.md
...n-dev/file-management/medialibrary-filepath-guidelines.md
+53
-48
zh-cn/application-dev/file-management/medialibrary-overview.md
.../application-dev/file-management/medialibrary-overview.md
+14
-14
zh-cn/application-dev/file-management/medialibrary-resource-guidelines.md
...n-dev/file-management/medialibrary-resource-guidelines.md
+78
-68
未找到文件。
zh-cn/application-dev/file-management/medialibrary-album-guidelines.md
浏览文件 @
911a4683
...
...
@@ -46,10 +46,10 @@ async function example() {
const
path
=
await
media
.
getPublicDirectory
(
DIR_IMAGE
);
//myAlbum为新建文件保存路径,也是新建相册的名称
media
.
createAsset
(
mediaType
,
'
test.jpg
'
,
path
+
'
myAlbum/
'
,
(
err
,
fileAsset
)
=>
{
if
(
fileAsset
!
=
undefined
)
{
console
.
info
(
'
createAlbum successfully, message =
'
+
fileAsset
);
if
(
fileAsset
==
=
undefined
)
{
console
.
error
(
'
createAlbum failed, message =
'
+
err
);
}
else
{
console
.
info
(
'
createAlbum failed, message =
'
+
err
);
console
.
info
(
'
createAlbum successfully, message =
'
+
JSON
.
stringify
(
fileAsset
)
);
}
});
}
...
...
@@ -85,10 +85,10 @@ async function example() {
let
album
=
albumList
[
0
];
album
.
albumName
=
'
newAlbum
'
;
//回调返回空
album
.
commitModify
().
then
(
function
()
{
album
.
commitModify
().
then
(
()
=>
{
console
.
info
(
"
albumRename successfully
"
);
}).
catch
(
function
(
err
)
{
console
.
info
(
"
albumRename failed with error:
"
+
err
);
}).
catch
(
(
err
)
=>
{
console
.
error
(
"
albumRename failed with error:
"
+
err
);
});
}
```
zh-cn/application-dev/file-management/medialibrary-filepath-guidelines.md
浏览文件 @
911a4683
...
...
@@ -30,7 +30,7 @@ Openharmony上用户数据统一由媒体库进行管理,用户数据用户数
**前提条件**
-
获取媒体库mediaLibrary实例。
-
申请媒体库读权限
“ohos.permission.READ_MEDIA
。
-
申请媒体库读权限
"ohos.permission.READ_MEDIA"
。
下面以获取Camera文件保存的公共目录为例。
...
...
@@ -43,7 +43,7 @@ async function example(){
if
(
dicResult
==
'
Camera/
'
)
{
console
.
info
(
'
mediaLibraryTest : getPublicDirectory passed
'
);
}
else
{
console
.
info
(
'
mediaLibraryTest : getPublicDirectory failed
'
);
console
.
error
(
'
mediaLibraryTest : getPublicDirectory failed
'
);
}
}
```
...
...
@@ -58,47 +58,52 @@ OpenHarmony提供应用沙箱机制,增加目录可见性数据访问防线,
通过接口
[
mediaLibrary.FileAsset.open
](
../reference/apis/js-apis-medialibrary.md#open8-1
)
可以打开公共路径文件。
通过接口
[
f
ileio.open
](
../reference/apis/js-apis-fileio.md#fileioopen7
)
可以打开沙箱路径文件,沙箱路径必须通过应用上下文context进行访问。
通过接口
[
f
s.open
](
../reference/apis/js-apis-file-fs.md#fsopen
)
可以打开沙箱路径文件,沙箱路径必须通过应用上下文context进行访问。
**前提条件**
-
获取媒体库mediaLibrary实例。
-
申请媒体库读写权限“ohos.permission.WRITE_MEDIA。
-
除了@ohos.multimedia.mediaLibrary外,还需要导入模块
[
@ohos.fileio
](
../reference/apis/js-apis-fileio.md
)
。
-
申请媒体库读写权限"ohos.permission.READ_MEDIA, ohos.permission.WRITE_MEDIA"。
-
除了@ohos.multimedia.mediaLibrary外,还需要导入模块
[
@ohos.file.fs
](
../reference/apis/js-apis-file-fs.md
)
。
-
测试文件 "testFile.txt" 已创建且有文件内容。
**开发步骤**
1.
调用
[
context.filesDir
](
../reference/apis/js-apis-
inner-app-context.md#contextgetfilesdir
)
获取应用沙箱路径。
1.
调用
[
context.filesDir
](
../reference/apis/js-apis-
file-fs.md
)
获取应用沙箱路径。
2.
调用MediaLibrary.getFileAssets和FetchFileResult.getFirstObject获取公共目录中的FileAsset实例。
3.
调用f
ileio
.open打开沙箱路径文件。
3.
调用f
s
.open打开沙箱路径文件。
4.
调用fileAsset.open打开公共路径文件。
5.
调用
fileio.copyfile
复制文件。
6.
调用fileAsset.close和
fileio.close
关闭文件。
5.
调用
[
fs.copyfile
](
../reference/apis/js-apis-file-fs.md#fscopyfile
)
复制文件。
6.
调用fileAsset.close和
[
fs.close
](
../reference/apis/js-apis-file-fs.md#fsclose
)
关闭文件。
**示例1 将公共路径文件复制到沙箱路径下**
```
ts
async
function
copyPublic2Sandbox
()
{
const
context
=
getContext
(
this
);
let
media
=
mediaLibrary
.
getMediaLibrary
(
context
);
let
sandboxDirPath
=
globalThis
.
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
();
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
fileio
.
open
(
sandboxDirPath
+
'
/testFile.txt
'
,
0o2
|
0o100
,
0o666
);
await
fileio
.
copyFile
(
fdPub
,
fdSan
d
);
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
.
f
d
);
await
fileAsset
.
close
(
fdPub
);
await
fileio
.
close
(
fdSan
d
);
await
fileAsset
.
close
(
fdPub
);
await
fs
.
close
(
fdSand
.
f
d
);
let
content_sand
=
await
fileio
.
readText
(
sandboxDirPath
+
'
/testFile.txt
'
);
console
.
log
(
'
content read from sandbox file:
'
,
content_sand
)
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
);
}
}
```
...
...
@@ -108,7 +113,7 @@ async function copyPublic2Sandbox() {
async
function
copySandbox2Public
()
{
const
context
=
getContext
(
this
);
let
media
=
mediaLibrary
.
getMediaLibrary
(
context
);
let
sandboxDirPath
=
globalThis
.
context
.
filesDir
;
let
sandboxDirPath
=
context
.
filesDir
;
let
DIR_DOCUMENTS
=
mediaLibrary
.
DirectoryType
.
DIR_DOCUMENTS
;
const
publicDirPath
=
await
media
.
getPublicDirectory
(
DIR_DOCUMENTS
);
...
...
@@ -116,7 +121,7 @@ async function copySandbox2Public() {
let
fileAsset
=
await
media
.
createAsset
(
mediaLibrary
.
MediaType
.
FILE
,
'
testFile02.txt
'
,
publicDirPath
);
console
.
info
(
'
createFile successfully, message =
'
+
fileAsset
);
}
catch
(
err
)
{
console
.
info
(
'
createFile failed, message =
'
+
err
);
console
.
error
(
'
createFile failed, message =
'
+
err
);
}
try
{
let
fileKeyObj
=
mediaLibrary
.
FileKey
;
...
...
@@ -127,35 +132,35 @@ async function copySandbox2Public() {
let
fetchResult
=
await
media
.
getFileAssets
(
fileAssetFetchOp
);
var
fileAsset
=
await
fetchResult
.
getFirstObject
();
}
catch
(
err
)
{
console
.
info
(
'
file asset get failed, message =
'
+
err
);
console
.
error
(
'
file asset get failed, message =
'
+
err
);
}
let
fdPub
=
await
fileAsset
.
open
(
'
rw
'
);
let
fdSand
=
await
f
ileio
.
open
(
sandboxDirPath
+
'
testFile.txt
'
,
0o2
);
await
f
ileio
.
copyFile
(
fdSan
d
,
fdPub
);
await
file
io
.
close
(
fdPub
);
await
f
ileio
.
close
(
fdSan
d
);
let
fdSand
=
await
f
s
.
open
(
sandboxDirPath
+
'
testFile.txt
'
,
OpenMode
.
READ_WRITE
);
await
f
s
.
copyFile
(
fdSand
.
f
d
,
fdPub
);
await
file
Asset
.
close
(
fdPub
);
await
f
s
.
close
(
fdSand
.
f
d
);
let
fdPubRead
=
await
fileAsset
.
open
(
'
rw
'
);
try
{
let
arrayBuffer
=
new
ArrayBuffer
(
4096
);
await
f
ileio
.
read
(
fdPubRead
,
arrayBuffer
);
await
f
s
.
read
(
fdPubRead
,
arrayBuffer
);
var
content_pub
=
String
.
fromCharCode
(...
new
Uint8Array
(
arrayBuffer
));
fileAsset
.
close
(
fdPubRead
);
}
catch
(
err
)
{
console
.
log
(
'
read text failed, message =
'
,
err
);
console
.
error
(
'
read text failed, message =
'
,
err
);
}
console
.
log
(
'
content read from public file:
'
,
content_pub
);
console
.
info
(
'
content read from public file:
'
,
content_pub
);
}
```
### 读写文件内容
通过
[
mediaLibrary
](
../reference/apis/js-apis-medialibrary.md
)
的接口FileAsset.open和FileAsset.close可以打开和关闭文件。通过
[
file
io
](
../reference/apis/js-apis-fileio.md
)
的接口fileio.read和fileio
.write可以读写文件。
通过
[
mediaLibrary
](
../reference/apis/js-apis-medialibrary.md
)
的接口FileAsset.open和FileAsset.close可以打开和关闭文件。通过
[
file
.fs
](
../reference/apis/js-apis-file-fs.md
)
中的接口fs.read和fs
.write可以读写文件。
**前提条件**
-
获取媒体库mediaLibrary实例。
-
申请媒体库读写权限
“ohos.permission.WRITE_MEDIA
。
-
除了@ohos.multimedia.mediaLibrary外,还需要导入模块
[
@ohos.file
io
](
../reference/apis/js-apis-fileio
.md
)
。
-
申请媒体库读写权限
"ohos.permission.READ_MEDIA, ohos.permission.WRITE_MEDIA"
。
-
除了@ohos.multimedia.mediaLibrary外,还需要导入模块
[
@ohos.file
.fs
](
../reference/apis/js-apis-file-fs
.md
)
。
**开发步骤**
...
...
@@ -168,19 +173,19 @@ async function copySandbox2Public() {
const
context
=
getContext
(
this
);
let
media
=
mediaLibrary
.
getMediaLibrary
(
context
);
const
path
=
await
media
.
getPublicDirectory
(
DIR_DOCUMENTS
);
media
.
createAsset
(
mediaType
,
"
testFile.text
"
,
path
).
then
(
function
(
asset
)
{
media
.
createAsset
(
mediaType
,
"
testFile.text
"
,
path
).
then
((
asset
)
=>
{
console
.
info
(
"
createAsset successfully:
"
+
JSON
.
stringify
(
asset
));
}).
catch
(
function
(
err
)
{
console
.
info
(
"
createAsset failed with error:
"
+
err
);
}).
catch
(
(
err
)
=>
{
console
.
error
(
"
createAsset failed with error:
"
+
err
);
});
}
```
2.
使用open打开文件。
3.
使用
fileio.write
写入文件,以string形式传入写入数据。
3.
使用
[
fs.write
](
../reference/apis/js-apis-file-fs.md#fswrite
)
写入文件,以string形式传入写入数据。
4.
使用
fileio.read
读取文件,以 ArrayBuffer 形式保存读取结果。
4.
使用
[
fs.read
](
../reference/apis/js-apis-file-fs.md#fsread
)
读取文件,以 ArrayBuffer 形式保存读取结果。
5.
将ArrayBuffer转化为string,以string形式得到文件内容。
...
...
@@ -204,10 +209,10 @@ async function writeOnlyPromise() {
try
{
let
fd
=
await
fileAsset
.
open
(
'
w
'
);
console
.
info
(
'
file descriptor:
'
,
fd
);
await
f
ileio
.
write
(
fd
,
"
Write file test content.
"
);
await
f
s
.
write
(
fd
,
"
Write file test content.
"
);
await
fileAsset
.
close
(
fd
);
}
catch
(
err
)
{
console
.
info
(
'
write file failed, message =
'
,
err
);
console
.
error
(
'
write file failed, message =
'
,
err
);
}
}
```
...
...
@@ -230,14 +235,14 @@ async function readOnlyPromise() {
try
{
let
fd
=
await
fileAsset
.
open
(
'
r
'
);
let
arrayBuffer
=
new
ArrayBuffer
(
4096
);
await
f
ileio
.
read
(
fd
,
arrayBuffer
);
await
f
s
.
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
.
info
(
'
read file failed, message =
'
,
err
);
console
.
error
(
'
read file failed, message =
'
,
err
);
}
}
```
...
...
zh-cn/application-dev/file-management/medialibrary-overview.md
浏览文件 @
911a4683
...
...
@@ -109,19 +109,19 @@ let media = mediaLibrary.getMediaLibrary(context);
import
abilityAccessCtrl
,
{
Permissions
}
from
'
@ohos.abilityAccessCtrl
'
;
export
default
class
EntryAbility
extends
UIAbility
{
onWindowStageCreate
(
windowStage
)
{
let
list
:
Array
<
Permissions
>
=
[
'
ohos.permission.READ_MEDIA
'
,
'
ohos.permission.WRITE_MEDIA
'
];
let
permissionRequestResult
;
let
atManager
=
abilityAccessCtrl
.
createAtManager
();
atManager
.
requestPermissionsFromUser
(
this
.
context
,
list
,
(
err
,
result
)
=>
{
if
(
err
)
{
console
.
log
(
'
requestPermissionsFromUserError:
'
+
JSON
.
stringify
(
err
));
}
else
{
permissionRequestResult
=
result
;
console
.
log
(
'
permissionRequestResult:
'
+
JSON
.
stringify
(
permissionRequestResult
));
}
});
}
}
onWindowStageCreate
(
windowStage
)
{
let
list
:
Array
<
Permissions
>
=
[
'
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
));
}
});
}
}
```
zh-cn/application-dev/file-management/medialibrary-resource-guidelines.md
浏览文件 @
911a4683
...
...
@@ -42,21 +42,24 @@ async function example() {
const
context
=
getContext
(
this
);
let
media
=
mediaLibrary
.
getMediaLibrary
(
context
);
const
fetchFileResult
=
await
media
.
getFileAssets
(
option
);
for
(
let
i
=
0
;
i
<
fetchFileResult
.
getCount
();
i
++
)
{
fetchFileResult
.
getNextObject
((
err
,
fileAsset
)
=>
{
if
(
err
)
{
console
.
error
(
'
Failed
'
);
return
;
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
);
});
}
console
.
log
(
'
fileAsset.displayName
'
+
i
+
'
:
'
+
fileAsset
.
displayName
);
})
}
}).
catch
((
err
)
=>
{
console
.
error
(
'
Failed to get first object:
'
+
err
);
}
);
}
```
### 指定日期
下面以查询指定添加日期
的
媒体资源为例。实际开发中可以设置添加日期、修改日期、拍摄日期。
下面以查询指定添加日期
至今的所有
媒体资源为例。实际开发中可以设置添加日期、修改日期、拍摄日期。
selections: FileKey.DATE_ADDED,根据文件添加日期检索。
...
...
@@ -66,21 +69,24 @@ selectionArgs:2022-8-5,具体添加时间的字符串。
async
function
example
()
{
let
fileKeyObj
=
mediaLibrary
.
FileKey
;
let
option
=
{
selections
:
fileKeyObj
.
DATE_ADDED
+
'
=
?
'
,
selectionArgs
:
[
'
2022-8-5
'
],
selections
:
fileKeyObj
.
DATE_ADDED
+
'
>
?
'
,
selectionArgs
:
[
'
2022-8-5
'
],
};
const
context
=
getContext
(
this
);
let
media
=
mediaLibrary
.
getMediaLibrary
(
context
);
const
fetchFileResult
=
await
media
.
getFileAssets
(
option
);
for
(
let
i
=
0
;
i
<
fetchFileResult
.
getCount
();
i
++
)
{
fetchFileResult
.
getNextObject
((
err
,
fileAsset
)
=>
{
if
(
err
)
{
console
.
error
(
'
Failed
'
);
return
;
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
);
});
}
console
.
log
(
'
fileAsset.displayName
'
+
i
+
'
:
'
+
fileAsset
.
displayName
);
})
}
}).
catch
((
err
)
=>
{
console
.
error
(
'
Failed to get first object:
'
+
err
);
}
);
}
```
...
...
@@ -102,15 +108,18 @@ async function example() {
const
context
=
getContext
(
this
);
let
media
=
mediaLibrary
.
getMediaLibrary
(
context
);
const
fetchFileResult
=
await
media
.
getFileAssets
(
option
);
for
(
let
i
=
0
;
i
<
fetchFileResult
.
getCount
();
i
++
)
{
fetchFileResult
.
getNextObject
((
err
,
fileAsset
)
=>
{
if
(
err
)
{
console
.
error
(
'
Failed
'
);
return
;
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
);
});
}
console
.
log
(
'
fileAsset.displayName
'
+
i
+
'
:
'
+
fileAsset
.
displayName
);
})
}
}).
catch
((
err
)
=>
{
console
.
error
(
'
Failed to get first object:
'
+
err
);
}
);
}
```
...
...
@@ -133,15 +142,15 @@ async function example() {
const
context
=
getContext
(
this
);
let
media
=
mediaLibrary
.
getMediaLibrary
(
context
);
const
fetchFileResult
=
await
media
.
getFileAssets
(
option
);
for
(
let
i
=
0
;
i
<
fetchFileResult
.
getCount
();
i
++
)
{
fetchFileResult
.
get
NextObject
((
err
,
fileAsset
)
=>
{
if
(
err
)
{
console
.
error
(
'
Failed
'
);
return
;
}
console
.
log
(
'
fileAsset.displayName
'
+
i
+
'
:
'
+
fileAsset
.
displayName
);
})
}
if
(
albumList
.
length
>
0
)
{
fetchFileResult
.
get
FirstObject
().
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
'
);
}
}
```
...
...
@@ -172,10 +181,10 @@ async function example() {
```
ts
let
fileKeyObj
=
mediaLibrary
.
FileKey
;
let
image
Type
=
mediaLibrary
.
MediaType
.
VIDEO
;
let
images
FetchOp
=
{
let
video
Type
=
mediaLibrary
.
MediaType
.
VIDEO
;
let
video
FetchOp
=
{
selections
:
fileKeyObj
.
MEDIA_TYPE
+
'
= ?
'
,
selectionArgs
:
[
image
Type
.
toString
()],
selectionArgs
:
[
video
Type
.
toString
()],
}
```
...
...
@@ -188,10 +197,10 @@ async function getCameraImagePromise() {
const
context
=
getContext
(
this
);
let
media
=
mediaLibrary
.
getMediaLibrary
(
context
);
let
fileKeyObj
=
mediaLibrary
.
FileKey
;
let
imageType
=
mediaLibrary
.
MediaType
.
IMAGE
;
let
images
FetchOp
=
{
let
videoType
=
mediaLibrary
.
MediaType
.
VIDEO
;
let
video
FetchOp
=
{
selections
:
fileKeyObj
.
MEDIA_TYPE
+
'
= ?
'
,
selectionArgs
:
[
image
Type
.
toString
()],
selectionArgs
:
[
video
Type
.
toString
()],
}
let
AlbumNoArgsFetchOp
=
{
selections
:
fileKeyObj
.
ALBUM_NAME
+
'
= ?
'
,
...
...
@@ -201,9 +210,9 @@ async function getCameraImagePromise() {
let
albumList
=
await
media
.
getAlbums
(
AlbumNoArgsFetchOp
);
if
(
albumList
.
length
>
0
)
{
const
album
=
albumList
[
0
];
let
fetchFileResult
=
await
album
.
getFileAssets
(
images
FetchOp
);
let
fetchFileResult
=
await
album
.
getFileAssets
(
video
FetchOp
);
let
count
=
fetchFileResult
.
getCount
();
console
.
info
(
"
get mediaLibrary
IMAGE
number
"
,
count
);
console
.
info
(
"
get mediaLibrary
VIDEO
number
"
,
count
);
}
else
{
console
.
info
(
'
getAlbum list is: 0
'
);
}
...
...
@@ -245,19 +254,20 @@ async function getFirstThumbnailPromise() {
let
size
=
{
width
:
720
,
height
:
720
};
const
fetchFileResult
=
await
media
.
getFileAssets
(
imagesFetchOp
);
if
(
fetchFileResult
!=
undefined
)
{
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
)
=>
{
pixelMap
.
getImageInfo
().
then
((
info
)
=>
{
console
.
info
(
'
get Thumbnail info:
'
+
"
width:
"
+
info
.
size
.
width
+
"
height:
"
+
info
.
size
.
height
);
}).
catch
((
err
)
=>
{
console
.
info
(
"
getImageInfo failed with error:
"
+
err
);
});
}).
catch
((
err
)
=>
{
console
.
error
(
"
getImageInfo failed with error:
"
+
err
);
});
}).
catch
((
err
)
=>
{
console
.
info
(
"
getImageInfo failed with error:
"
+
err
);
console
.
error
(
"
getImageInfo failed with error:
"
+
err
);
});
}
else
{
console
.
info
(
"
get image failed with error
"
);
}
}
```
...
...
@@ -281,10 +291,10 @@ async function example() {
const
context
=
getContext
(
this
);
let
media
=
mediaLibrary
.
getMediaLibrary
(
context
);
const
path
=
await
media
.
getPublicDirectory
(
DIR_DOCUMENTS
);
media
.
createAsset
(
mediaType
,
"
testFile.text
"
,
path
).
then
((
asset
)
=>
{
media
.
createAsset
(
mediaType
,
"
testFile.text
"
,
path
).
then
((
asset
)
=>
{
console
.
info
(
"
createAsset successfully:
"
+
JSON
.
stringify
(
asset
));
}).
catch
((
err
)
=>
{
console
.
info
(
"
createAsset failed with error:
"
+
err
);
console
.
error
(
"
createAsset failed with error:
"
+
err
);
});
}
```
...
...
@@ -321,15 +331,15 @@ async function example() {
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
;
if
(
asset
==
=
undefined
)
{
console
.
error
(
'
asset not exist
'
);
return
;
}
//回调为空
asset
.
trash
(
true
).
then
(()
=>
{
console
.
info
(
"
trash successfully
"
);
}).
catch
((
err
)
=>
{
console
.
info
(
"
trash failed with error:
"
+
err
);
console
.
error
(
"
trash failed with error:
"
+
err
);
});
}
```
...
...
@@ -347,7 +357,7 @@ async function example() {
-
获取媒体库mediaLibrary实例。
-
申请媒体库读写权限“ohos.permission.WRITE_MEDIA”。
下面以将文件检索结果中第一个文件重命名为“new
title.text
”为例。
下面以将文件检索结果中第一个文件重命名为“new
Image.jpg
”为例。
**开发步骤**
...
...
@@ -360,7 +370,7 @@ async function example() {
```
ts
async
function
example
()
{
let
fileKeyObj
=
mediaLibrary
.
FileKey
;
let
fileType
=
mediaLibrary
.
MediaType
.
FIL
E
;
let
fileType
=
mediaLibrary
.
MediaType
.
IMAG
E
;
let
option
=
{
selections
:
fileKeyObj
.
MEDIA_TYPE
+
'
= ?
'
,
selectionArgs
:
[
fileType
.
toString
()],
...
...
@@ -369,18 +379,18 @@ async function example() {
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
;
if
(
asset
==
=
undefined
)
{
console
.
error
(
'
asset not exist
'
);
return
;
}
asset
.
displayName
=
'
newImage.jpg
'
;
//回调为空
asset
.
commitModify
((
err
)
=>
{
if
(
err
)
{
console
.
error
(
'
fileRename Failed
'
);
return
;
}
console
.
log
(
'
fileRename successful.
'
);
if
(
err
)
{
console
.
error
(
'
fileRename Failed
'
);
return
;
}
console
.
info
(
'
fileRename successful.
'
);
});
}
```
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录