Skip to content

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 ​

PropertyTypeRequiredDescription
countnumberNoMaximum number of images to select; pass a positive integer explicitly. The number that can actually be selected depends on the current runtime
sizeTypeArray<string>NoImage size types: original or compressed. Specify the required type explicitly
sourceTypeArray<string>YesImage sources; include at least one of album or camera. Both can be supplied
successfunctionNoCalled when image selection or capture succeeds
failfunctionNoCalled when selection or capture fails, including user cancellation
completefunctionNoCalled 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 ​

PropertyTypeDescription
tempFilePathsArray<string>Local temporary file paths of the selected images
tempFilesArray<Object>File information for the selected images

Each item in tempFiles has the following structure:

PropertyTypeDescription
pathstringLocal temporary image file path

fail ​

PropertyTypeDescription
errCodenumberError code
errMsgstringError message

complete ​

Called after success or fail, with no arguments.

Usage notes ​

  • Additional file information in tempFiles may vary by runtime environment. For image paths across environments, prefer tempFilePaths.
  • Use the actual length of tempFilePaths or tempFiles. Do not assume that the number returned always equals count.
  • When both original and compressed images are allowed, the returned type depends on the current runtime. Supply only one sizeType when a specific result is needed.
  • Handle fail when 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');
    },
});