sud.chooseImage
Select images from the album or take a photo and obtain local temporary image file paths.
javascript
sud.chooseImage(object)Parameters
Object object
| Property | Type | Required | Description |
|---|---|---|---|
| count | number | No | Maximum number of images to select; pass a positive integer explicitly. The number that can actually be selected depends on the current runtime |
| sizeType | Array<string> | No | Image size types: original or compressed. Specify the required type explicitly |
| sourceType | Array<string> | Yes | Image sources; include at least one of album or camera. Both can be supplied |
| success | function | No | Called when image selection or capture succeeds |
| fail | function | No | Called when selection or capture fails, including user cancellation |
| complete | function | No | Called when the request finishes, whether successful or failed |
There are no uniform default values for count and sizeType. Specify count, sizeType, and sourceType explicitly instead of relying on behavior when they are omitted.
Return value
This asynchronous API returns the selection result through success.
Callback parameters
success
| Property | Type | Description |
|---|---|---|
| tempFilePaths | Array<string> | Local temporary file paths of the selected images |
| tempFiles | Array<Object> | File information for the selected images |
Each item in tempFiles has the following structure:
| Property | Type | Description |
|---|---|---|
| path | string | Local temporary image file path |
fail
| Property | Type | Description |
|---|---|---|
| errCode | number | Error code |
| errMsg | string | Error message |
complete
Called after success or fail, with no arguments.
Usage notes
- Additional file information in
tempFilesmay vary by runtime environment. For image paths across environments, prefertempFilePaths. - Use the actual length of
tempFilePathsortempFiles. Do not assume that the number returned always equalscount. - When both original and compressed images are allowed, the returned type depends on the current runtime. Supply only one
sizeTypewhen a specific result is needed. - Handle
failwhen an image source is unavailable, the user cancels, permission is denied, or image processing fails. - Returned paths are local temporary file paths, not network URLs. To save an image to the system photo album, call sud.saveImageToPhotosAlbum.
- Avoid starting another selection before the previous request finishes.
Example
Select one original image from the album and preview the result.
javascript
sud.chooseImage({
count: 1,
sizeType: ['original'],
sourceType: ['album'],
success: (res) => {
const paths = res.tempFilePaths;
if (!paths || paths.length === 0) {
return;
}
sud.previewImage({
current: paths[0],
urls: paths,
fail: (err) => {
console.error('Preview failed', err.errCode, err.errMsg);
},
});
},
fail: (err) => {
console.error('Selection failed or was cancelled', err.errCode, err.errMsg);
},
complete: () => {
console.log('Selection request completed');
},
});