“39266cc6f9b19d314e65faca847cd446d14c970f”上不存在“python/git@gitcode.net:s920243400/PaddleDetection.git”
未验证 提交 a00abc7e 编写于 作者: O openharmony_ci 提交者: Gitee

!19764 [翻译完成】#I789BD

Merge pull request !19764 from Annie_wang/PR18601
...@@ -7,10 +7,11 @@ The operations for saving images, audio or video clips, and documents are simila ...@@ -7,10 +7,11 @@ The operations for saving images, audio or video clips, and documents are simila
## Saving Images or Video Files ## Saving Images or Video Files
1. Import the **FilePicker** module. 1. Import the **picker** module and **fs** module.
```ts ```ts
import picker from '@ohos.file.picker'; import picker from '@ohos.file.picker';
import fs from '@ohos.file.fs';
``` ```
2. Create a **photoSaveOptions** instance. 2. Create a **photoSaveOptions** instance.
...@@ -20,27 +21,43 @@ The operations for saving images, audio or video clips, and documents are simila ...@@ -20,27 +21,43 @@ The operations for saving images, audio or video clips, and documents are simila
photoSaveOptions.newFileNames = ["PhotoViewPicker01.jpg"]; // (Optional) Set the names of the files to save. photoSaveOptions.newFileNames = ["PhotoViewPicker01.jpg"]; // (Optional) Set the names of the files to save.
``` ```
3. Create a **photoViewPicker** instance and call [save()](../reference/apis/js-apis-file-picker.md#save) to open the **FilePicker** page to save the files. 3. Create a **photoViewPicker** instance and call [save()](../reference/apis/js-apis-file-picker.md#save) to open the **FilePicker** page to save the files. After the user selects the target folder, the file saving operation is complete. After the files are saved successfully, the URIs of the files saved are returned.
After the user selects the target folder, the file saving operation is complete. After the files are saved successfully, the URIs of the files saved are returned.
<br>The permission on the URIs returned by **save()** is read/write. Further file operations can be performed based on the URIs in the result set. Note that the URI cannot be directly used in the **picker** callback to open a file. You need to define a global variable to save the URI and use a button to trigger file opening.
```ts ```ts
let URI = null;
const photoViewPicker = new picker.PhotoViewPicker(); const photoViewPicker = new picker.PhotoViewPicker();
photoViewPicker.save(photoSaveOptions) photoViewPicker.save(photoSaveOptions).then((photoSaveResult) => {
.then(async (photoSaveResult) => { URI = photoSaveResult[0];
let uri = photoSaveResult[0]; console.info('photoViewPicker.save to file succeed and URI is:' + URI);
// Perform operations on the files based on the file URIs obtained. }).catch((err) => {
}) console.error(`Invoke photoViewPicker.save failed, code is ${err.code}, message is ${err.message}`);
.catch((err) => { })
console.error(`Invoke documentPicker.select failed, code is ${err.code}, message is ${err.message}`); ```
})
4. Use a button to trigger invocation of other functions. Use [fs.openSync()](../reference/apis/js-apis-file-fs.md#fsopensync) to open the file based on the URI and obtain the FD. Note that the **mode** parameter of **fs.openSync()** must be **fs.OpenMode.READ_WRITE**.
```ts
let file = fs.openSync(URI, fs.OpenMode.READ_WRITE);
console.info('file fd: ' + file.fd);
```
5. Use [fs.writeSync()](../reference/apis/js-apis-file-fs.md#writesync) to edit the file based on the FD, and then close the FD.
```ts
let writeLen = fs.writeSync(file.fd, 'hello, world');
console.info('write data to file succeed and size is:' + writeLen);
fs.closeSync(file);
``` ```
## Saving Documents ## Saving Documents
1. Import the **FilePicker** module. 1. Import the **picker** module and **fs** module.
```ts ```ts
import picker from '@ohos.file.picker'; import picker from '@ohos.file.picker';
import fs from '@ohos.file.fs';
``` ```
2. Create a **documentSaveOptions** instance. 2. Create a **documentSaveOptions** instance.
...@@ -50,31 +67,43 @@ The operations for saving images, audio or video clips, and documents are simila ...@@ -50,31 +67,43 @@ The operations for saving images, audio or video clips, and documents are simila
documentSaveOptions.newFileNames = ["DocumentViewPicker01.txt"]; // (Optional) Set the names of the documents to save. documentSaveOptions.newFileNames = ["DocumentViewPicker01.txt"]; // (Optional) Set the names of the documents to save.
``` ```
3. Create a **documentViewPicker** instance, and call [save()](../reference/apis/js-apis-file-picker.md#save-3) to open the **FilePicker** page to save the documents. 3. Create a **documentViewPicker** instance, and call [save()](../reference/apis/js-apis-file-picker.md#save-3) to open the **FilePicker** page to save the documents. After the user selects the target folder, the file saving operation is complete. After the files are saved successfully, the URIs of the files saved are returned.
After the user selects the target folder, the file saving operation is complete. After the files are saved successfully, the URIs of the files saved are returned.
The permission on the URIs returned by **save()** is read/write. Further file operations can be performed based on the URIs in the result set. Note that the URI cannot be directly used in the **picker** callback to open a file. You need to define a global variable to save the URI and use a button to trigger file opening.
> **NOTE**
>
> Currently, **DocumentSelectOptions** is not configurable. By default, all types of user files are selected.
```ts ```ts
let URI = null;
const documentViewPicker = new picker.DocumentViewPicker(); // Create a documentViewPicker instance. const documentViewPicker = new picker.DocumentViewPicker(); // Create a documentViewPicker instance.
documentViewPicker.save(documentSaveOptions) documentViewPicker.save(documentSaveOptions).then((documentSaveResult) => {
.then(async (documentSaveResult) => { URI = documentSaveResult[0];
let uri = documentSaveResult[0]; console.info('documentViewPicker.save to file succeed and URI is:' + URI);
// For example, write data to the documents based on the obtained URIs. }).catch((err) => {
}) console.error(`Invoke documentViewPicker.save failed, code is ${err.code}, message is ${err.message}`);
.catch((err) => { })
console.error(`Invoke documentPicker.save failed, code is ${err.code}, message is ${err.message}`); ```
})
4. Use a button to trigger invocation of other functions. Use [fs.openSync()](../reference/apis/js-apis-file-fs.md#fsopensync) to open the file based on the URI and obtain the FD. Note that the **mode** parameter of **fs.openSync()** must be **fs.OpenMode.READ_WRITE**.
```ts
let file = fs.openSync(URI, fs.OpenMode.READ_WRITE);
console.info('file fd: ' + file.fd);
```
5. Use [fs.writeSync()](../reference/apis/js-apis-file-fs.md#writesync) to edit the file based on the FD, and then close the FD.
```ts
let writeLen = fs.writeSync(file.fd, 'hello, world');
console.info('write data to file succeed and size is:' + writeLen);
fs.closeSync(file);
``` ```
## Saving Audio Files ## Saving Audio Files
1. Import the **FilePicker** module. 1. Import the **picker** module and **fs** module.
```ts ```ts
import picker from '@ohos.file.picker'; import picker from '@ohos.file.picker';
import fs from '@ohos.file.fs';
``` ```
2. Create an **audioSaveOptions** instance. 2. Create an **audioSaveOptions** instance.
...@@ -84,20 +113,33 @@ The operations for saving images, audio or video clips, and documents are simila ...@@ -84,20 +113,33 @@ The operations for saving images, audio or video clips, and documents are simila
audioSaveOptions.newFileNames = ['AudioViewPicker01.mp3']; // (Optional) Set the names of the files to save. audioSaveOptions.newFileNames = ['AudioViewPicker01.mp3']; // (Optional) Set the names of the files to save.
``` ```
3. Create an **audioViewPicker** instance, and call [save()](../reference/apis/js-apis-file-picker.md#save-6) to open the **FilePicker** page to save the files. 3. Create an **audioViewPicker** instance, and call [save()](../reference/apis/js-apis-file-picker.md#save-6) to open the **FilePicker** page to save the files. After the user selects the target folder, the file saving operation is complete. After the files are saved successfully, the URIs of the files saved are returned.
After the user selects the target folder, the file saving operation is complete. After the files are saved successfully, the URIs of the files saved are returned.
> **NOTE** The permission on the URIs returned by **save()** is read/write. Further file operations can be performed based on the URIs in the result set. Note that the URI cannot be directly used in the **picker** callback to open a file. You need to define a global variable to save the URI and use a button to trigger file opening.
>
> Currently, **AudioSelectOptions** is not configurable. By default, all types of user files are selected.
```ts ```ts
let URI = null;
const audioViewPicker = new picker.AudioViewPicker(); const audioViewPicker = new picker.AudioViewPicker();
audioViewPicker.save(audioSaveOptions) audioViewPicker.save(audioSaveOptions).then((audioSelectResult) => {
.then((audioSelectResult) => { URI = audioSelectResult[0];
let uri = audioSelectResult[0]; console.info('audioViewPicker.save to file succeed and URI is:' + URI);
// Perform operations on the audio files based on the file URIs. }).catch((err) => {
}) console.error(`Invoke audioViewPicker.save failed, code is ${err.code}, message is ${err.message}`);
.catch((err) => { })
console.error(`Invoke audioPicker.select failed, code is ${err.code}, message is ${err.message}`); ```
})
4. Use a button to trigger invocation of other functions. Use [fs.openSync()](../reference/apis/js-apis-file-fs.md#fsopensync) to open the file based on the URI and obtain the FD. Note that the **mode** parameter of **fs.openSync()** must be **fs.OpenMode.READ_WRITE**.
```ts
let file = fs.openSync(URI, fs.OpenMode.READ_WRITE);
console.info('file fd: ' + file.fd);
```
5. Use [fs.writeSync](../reference/apis/js-apis-file-fs.md#writesync) to edit the file based on the FD, and then close the FD.
```ts
let writeLen = fs.writeSync(file.fd, 'hello, world');
console.info('write data to file succeed and size is:' + writeLen);
fs.closeSync(file);
``` ```
# Selecting User Files # Selecting User Files
If your application needs to support share and saving of user files (such as images and videos) by users, you can use the [FilePicker](../reference/apis/js-apis-file-picker.md) prebuilt in OpenHarmony to implement selecting and saving of user files. If your application needs to support share and saving of user files (such as images and videos), you can use OpenHarmony [FilePicker](../reference/apis/js-apis-file-picker.md) to implement selection and saving of user files.
The **FilePicker** provides the following interfaces by file type: The **FilePicker** provides the following interfaces by file type:
...@@ -12,10 +12,11 @@ The **FilePicker** provides the following interfaces by file type: ...@@ -12,10 +12,11 @@ The **FilePicker** provides the following interfaces by file type:
## Selecting Images or Video Files ## Selecting Images or Video Files
1. Import the **FilePicker** module. 1. Import the **picker** module and **fs** module.
```ts ```ts
import picker from '@ohos.file.picker'; import picker from '@ohos.file.picker';
import fs from '@ohos.file.fs';
``` ```
2. Create a **photoSelectOptions** instance. 2. Create a **photoSelectOptions** instance.
...@@ -32,28 +33,44 @@ The **FilePicker** provides the following interfaces by file type: ...@@ -32,28 +33,44 @@ The **FilePicker** provides the following interfaces by file type:
photoSelectOptions.maxSelectNumber = 5; // Set the maximum number of images to select. photoSelectOptions.maxSelectNumber = 5; // Set the maximum number of images to select.
``` ```
4. Create a **photoPicker** instance and call [select()](../reference/apis/js-apis-file-picker.md#select) to open the **FilePicker** page for the user to select files. 4. Create a **photoPicker** instance and call [select()](../reference/apis/js-apis-file-picker.md#select) to open the **FilePicker** page for the user to select files. After the files are selected, [PhotoSelectResult](../reference/apis/js-apis-file-picker.md#photoselectresult) is returned.
<br>The permission on the URIs returned by **select()** is read-only. Further file operations can be performed based on the URIs in the **PhotoSelectResult**. Note that the URI cannot be directly used in the **picker** callback to open a file. You need to define a global variable to save the URI and use a button to trigger file opening.
Use [PhotoSelectResult](../reference/apis/js-apis-file-picker.md#photoselectresult) to return a result set. Further operations on the selected files can be performed based on the file URIs in the result set. ```ts
let URI = null;
const photoViewPicker = new picker.PhotoViewPicker();
photoViewPicker.select(photoSelectOptions).then((photoSelectResult) => {
URI = photoSelectResult.photoUris[0];
console.info('photoViewPicker.select to file succeed and URI is:' + URI);
}).catch((err) => {
console.error(`Invoke photoViewPicker.select failed, code is ${err.code}, message is ${err.message}`);
})
```
5. Use a button to trigger invocation of other functions. Use [fs.openSync()](../reference/apis/js-apis-file-fs.md#fsopensync) to open the file based on the URI and obtain the FD. Note that the **mode** parameter of **fs.openSync()** must be **fs.OpenMode.READ_ONLY**.
```ts
let file = fs.openSync(URI, fs.OpenMode.READ_ONLY);
console.info('file fd: ' + file.fd);
```
6. Use [fs.readSync()](../reference/apis/js-apis-file-fs.md#readsync) to read the file data based on the FD. After the data is read, close the FD.
```ts ```ts
const photoPicker = new picker.PhotoViewPicker(); let buffer = new ArrayBuffer(4096);
photoPicker.select(photoSelectOptions) let readLen = fs.readSync(file.fd, buffer);
.then(async (photoSelectResult) => { console.info('readSync data to file succeed and buffer size is:' + readLen);
let uri = photoSelectResult.photoUris[0]; fs.closeSync(file);
// Perform operations on the files based on the file URIs obtained.
})
.catch((err) => {
console.error(`Invoke documentPicker.select failed, code is ${err.code}, message is ${err.message}`);
})
``` ```
## Selecting Documents ## Selecting Documents
1. Import the **FilePicker** module. 1. Import the **picker** module and **fs** module.
```ts ```ts
import picker from '@ohos.file.picker'; import picker from '@ohos.file.picker';
import fs from '@ohos.file.fs';
``` ```
2. Create a **documentSelectOptions** instance. 2. Create a **documentSelectOptions** instance.
...@@ -62,26 +79,25 @@ The **FilePicker** provides the following interfaces by file type: ...@@ -62,26 +79,25 @@ The **FilePicker** provides the following interfaces by file type:
const documentSelectOptions = new picker.DocumentSelectOptions(); const documentSelectOptions = new picker.DocumentSelectOptions();
``` ```
3. Create a **documentViewPicker** instance, and call [**select()**](../reference/apis/js-apis-file-picker.md#select-3) to open the **FilePicker** page for the user to select documents. 3. Create a **documentViewPicker** instance, and call [**select()**](../reference/apis/js-apis-file-picker.md#select-3) to open the **FilePicker** page for the user to select documents. After the documents are selected, a result set containing the file URIs is returned.
After the documents are selected successfully, a result set containing the file URIs is returned. Further operations can be performed on the documents based on the file URIs. <br>The permission on the URIs returned by **select()** is read-only. Further file operations can be performed based on the URIs in the result set. Note that the URI cannot be directly used in the **picker** callback to open a file. You need to define a global variable to save the URI and use a button to trigger file opening.
For example, you can use [file management APIs](../reference/apis/js-apis-file-fs.md) to obtain file attribute information, such as the file size, access time, and last modification time, based on the URI. If you need to obtain the file name, use [startAbilityForResult](../../application-dev/application-models/uiability-intra-device-interaction.md). <br>For example, you can use [file management APIs](../reference/apis/js-apis-file-fs.md) to obtain file attribute information, such as the file size, access time, and last modification time, based on the URI. If you need to obtain the file name, use [startAbilityForResult](../../application-dev/application-models/uiability-intra-device-interaction.md).
> **NOTE** > **NOTE**
> >
> Currently, **DocumentSelectOptions** is not configurable. By default, all types of user files are selected. > Currently, **DocumentSelectOptions** is not configurable. By default, all types of user files are selected.
```ts ```ts
let URI = null;
const documentViewPicker = new picker.DocumentViewPicker(); // Create a documentViewPicker instance. const documentViewPicker = new picker.DocumentViewPicker(); // Create a documentViewPicker instance.
documentViewPicker.select(documentSelectOptions) documentViewPicker.select(documentSelectOptions).then((documentSelectResult) => {
.then((documentSelectResult) => { URI = documentSelectResult[0];
let uri = documentSelectResult[0]; console.info('documentViewPicker.select to file succeed and URI is:' + URI);
// Perform operations on the documents based on the file URIs. }).catch((err) => {
}) console.error(`Invoke documentViewPicker.select failed, code is ${err.code}, message is ${err.message}`);
.catch((err) => { })
console.error(`Invoke documentPicker.select failed, code is ${err.code}, message is ${err.message}`);
})
``` ```
> **NOTE** > **NOTE**
...@@ -98,7 +114,7 @@ The **FilePicker** provides the following interfaces by file type: ...@@ -98,7 +114,7 @@ The **FilePicker** provides the following interfaces by file type:
try { try {
let result = await context.startAbilityForResult(config, {windowMode: 1}); let result = await context.startAbilityForResult(config, {windowMode: 1});
if (result.resultCode !== 0) { if (result.resultCode !== 0) {
console.error(`DocumentPicker.select failed, code is ${result.resultCode}, message is ${result.want.parameters.message}`); console.error(`documentViewPicker.select failed, code is ${result.resultCode}, message is ${result.want.parameters.message}`);
return; return;
} }
// Obtain the URI of the document. // Obtain the URI of the document.
...@@ -106,16 +122,34 @@ The **FilePicker** provides the following interfaces by file type: ...@@ -106,16 +122,34 @@ The **FilePicker** provides the following interfaces by file type:
// Obtain the name of the document. // Obtain the name of the document.
let file_name_list = result.want.parameters.file_name_list; let file_name_list = result.want.parameters.file_name_list;
} catch (err) { } catch (err) {
console.error(`Invoke documentPicker.select failed, code is ${err.code}, message is ${err.message}`); console.error(`Invoke documentViewPicker.select failed, code is ${err.code}, message is ${err.message}`);
} }
``` ```
4. Use a button to trigger invocation of other functions. Use [fs.openSync()](../reference/apis/js-apis-file-fs.md#fsopensync) to open the file based on the URI and obtain the FD. Note that the **mode** parameter of **fs.openSync()** must be **fs.OpenMode.READ_ONLY**.
```ts
let file = fs.openSync(URI, fs.OpenMode.READ_ONLY);
console.info('file fd: ' + file.fd);
```
5. Use [fs.readSync()](../reference/apis/js-apis-file-fs.md#readsync) to read the file data based on the FD. After the data is read, close the FD.
```ts
let buffer = new ArrayBuffer(4096);
let readLen = fs.readSync(file.fd, buffer);
console.info('readSync data to file succeed and buffer size is:' + readLen);
fs.closeSync(file);
```
## Selecting an Audio File ## Selecting an Audio File
1. Import the **FilePicker** module. 1. Import the **picker** module and **fs** module.
```ts ```ts
import picker from '@ohos.file.picker'; import picker from '@ohos.file.picker';
import fs from '@ohos.file.fs';
``` ```
2. Create an **audioSelectOptions** instance. 2. Create an **audioSelectOptions** instance.
...@@ -124,24 +158,39 @@ The **FilePicker** provides the following interfaces by file type: ...@@ -124,24 +158,39 @@ The **FilePicker** provides the following interfaces by file type:
const audioSelectOptions = new picker.AudioSelectOptions(); const audioSelectOptions = new picker.AudioSelectOptions();
``` ```
3. Create an **audioViewPicker** instance, and call [**select()**](../reference/apis/js-apis-file-picker.md#select-6) to open the **FilePicker** page for the user to select audio files. 3. Create an **audioViewPicker** instance, and call [**select()**](../reference/apis/js-apis-file-picker.md#select-6) to open the **FilePicker** page for the user to select audio files. After the files are selected, a result set containing the URIs of the audio files selected is returned.
After the files are selected successfully, a result set containing the URIs of the audio files selected is returned. Further operations can be performed on the documents based on the file URIs. <br>The permission on the URIs returned by **select()** is read-only. Further file operations can be performed based on the URIs in the result set. Note that the URI cannot be directly used in the **picker** callback to open a file. You need to define a global variable to save the URI and use a button to trigger file opening.
For example, use the [file management interface](../reference/apis/js-apis-file-fs.md) to obtain the file handle (FD) of the audio clip based on the URI, and then develop the audio playback function based on the media service. For details, see [Audio Playback Development](../media/audio-playback-overview.md). <br>For example, use the [file management interface](../reference/apis/js-apis-file-fs.md) to obtain the file handle (FD) of the audio clip based on the URI, and then develop the audio playback function based on the media service. For details, see [Audio Playback Development](../media/audio-playback-overview.md).
> **NOTE** > **NOTE**
> >
> Currently, **AudioSelectOptions** is not configurable. By default, all types of user files are selected. > Currently, **AudioSelectOptions** is not configurable. By default, all types of user files are selected.
```ts ```ts
let URI = null;
const audioViewPicker = new picker.AudioViewPicker(); const audioViewPicker = new picker.AudioViewPicker();
audioViewPicker.select(audioSelectOptions) audioViewPicker.select(audioSelectOptions).then(audioSelectResult => {
.then(audioSelectResult => { URI = audioSelectOptions[0];
let uri = audioSelectOptions[0]; console.info('audioViewPicker.select to file succeed and URI is:' + URI);
// Perform operations on the audio files based on the file URIs. }).catch((err) => {
}) console.error(`Invoke audioViewPicker.select failed, code is ${err.code}, message is ${err.message}`);
.catch((err) => { })
console.error(`Invoke audioPicker.select failed, code is ${err.code}, message is ${err.message}`); ```
})
4. Use a button to trigger invocation of other functions. Use [fs.openSync()](../reference/apis/js-apis-file-fs.md#fsopensync) to open the file based on the URI and obtain the FD. Note that the **mode** parameter of **fs.openSync()** must be **fs.OpenMode.READ_ONLY**.
```ts
let file = fs.openSync(URI, fs.OpenMode.READ_ONLY);
console.info('file fd: ' + file.fd);
```
5. Use [fs.readSync()](../reference/apis/js-apis-file-fs.md#readsync) to read the file data based on the FD. After the data is read, close the FD.
```ts
let buffer = new ArrayBuffer(4096);
let readLen = fs.readSync(file.fd, buffer);
console.info('readSync data to file succeed and buffer size is:' + readLen);
fs.closeSync(file);
``` ```
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册