目录

Multimedia

API Declaration

Add the following configuration to the features attribute in the manifest.json file:

{"name": "system.media"}

Module Import

Add the following configuration to the script element on the page where the API will be called:

import media from '@system.media'

Or

var media = require("@system.media")

Application Scope

Item

Description

Applicable devices

Mobile phones and tablets

Applicable regions

Global

Definition

API

Description

media.takePhoto(OBJECT)

Takes a photo.

media.takeVideo(OBJECT)

Records a video.

media.saveToPhotosAlbum(OBJECT)

Saves image or video files to an album. User authorization is required.

media.pickFile(OBJECT)

Selects a file.

media.pickFiles(OBJECT)

Selects multiple files.

media.pickImage(OBJECT)

Selects an image.

media.pickImages(OBJECT)

Selects multiple images.

media.setRingtone(OBJECT)

Sets the ringtone.

media.getRingtone(OBJECT)

Obtains the ringtone.

media.pickVideo(OBJECT)

Selects a video.

media.pickVideos(OBJECT)

Selects multiple videos.

media.previewImage(OBJECT)

Previews an image. After the API is called, uploaded images are previewed in full-screen mode on a new page. Users can swipe left and right to preview these images, zoom in and out on the images using two fingers, and save the images to albums.

[h2]media.takePhoto(OBJECT)

Description

Takes a photo.

This API does not support Vision products.

OBJECT

Parameter

Type

Mandatory

Description

cancel

function

No

Callback upon cancellation.

success

function

No

Callback upon success.

fail

function

No

Callback upon failure.

complete

function

No

Callback upon completion.

Parameters returned in success

Parameter

Type

Description

uri

string

URI of the file.

name (1060+)

string

Name of the file.

size (1060+)

string

Size of the file, in bytes.

Result codes returned in fail

Result Code

Description

201

The user has rejected the request for the camera permission.

Sample code

media.takePhoto({  
  success: function(data) {
    console.log("handling success: " + data.uri);
  },
  fail: function(data, code) {
    console.log("handling fail, code=" + code);
  }
});

[h2]media.takeVideo(OBJECT)

Description

Records a video.

This API does not support Vision products.

OBJECT

Parameter

Type

Mandatory

Description

maxDuration (1070+)

number

No

Maximum duration for shooting a video, in seconds. The default value is 60.

cancel

function

No

Callback upon cancellation.

success

function

No

Callback upon success.

fail

function

No

Callback upon failure.

complete

function

No

Callback upon completion.

Parameters returned in success

Parameter

Type

Description

uri

string

URI of the file.

name (1060+)

string

Name of the file.

size (1060+)

string

Size of the file, in bytes.

Result codes returned in fail

Result Code

Description

201

The user has rejected the request for the camera permission.

Sample code

media.takeVideo({
    maxDuration: 10,
    success:function(data){console.log("handling success: " + data.uri);},
    fail: function(data, code) {
        console.log("handling fail, code=" + code);
    }
})

[h2]media.saveToPhotosAlbum(OBJECT) (1010+)

Description

Saves image or video files to an album. User authorization is required.

OBJECT

Parameter

Type

Mandatory

Description

uri

string

Yes

URI of the source file. The file name extension must be the name extension of an image or a video.

NOTE:

Only a local path is supported. If the file is an online image or video, call the file download API to download it to local, and then save it to the album.

folderName (1080+)

string

No

Name of the folder in the album where pictures or videos are saved. The maximum length of the name is 50 characters. A maximum of 10 levels of directories are supported. The name of each level of directory contains a maximum of 10 characters. Characters like backslashes (\), colons (:), asterisks (*), question marks (?), quotation marks ("), angle bracket (<>), and vertical bars (|) cannot be contained in the name.

NOTE:
  • If folderName is not set, the image storage address is in the format sdcard/Pictures/Quick app package name/Quick app name/xxx.jpg.
  • If folderName is set, the image storage address is in the format sdcard/Pictures/Quick app package name/folderName/yyy.jpg.

success

function

No

Callback upon success.

fail

function

No

Callback upon failure.

complete

function

No

Callback upon completion.

Result codes returned in fail

Result Code

Description

201

The operation is rejected by a user.

202

Invalid parameter.

300

I/O error.

Sample code

media.saveToPhotosAlbum({
    uri: 'internal://cache/123.png',
    folderName: 'custom-folder',
    success:function(data){console.log("save success: ");},
    fail: function(data, code) {
        console.log("handling fail, code=" + code);
    }
})

[h2]media.pickFile(OBJECT) (1010+)

Description

Selects a file.

OBJECT

Parameter

Type

Mandatory

Description

success

function

No

Callback upon success.

fail

function

No

Callback upon failure.

cancel

function

No

Callback upon cancellation.

complete

function

No

Callback upon completion.

Parameters returned in success

Parameter

Type

Description

uri

array

Path of the selected file.

name (1060+)

string

Name of the file.

size (1060+)

string

Size of the file, in bytes.

Sample code

media.pickFile({
    success: function (data) {
        console.log("handling success: " + data.uri)
    },
    fail: function (data, code) {
        console.log("handling fail, code=" + code)
    }
})

[h2]media.pickFiles(OBJECT) (1060+)

Description

Selects multiple files.

OBJECT

Parameter

Type

Mandatory

Description

success

function

No

Callback upon success.

fail

function

No

Callback upon failure.

cancel

function

No

Callback upon cancellation.

complete

function

No

Callback upon completion.

Parameters returned in success

Parameter

Type

Description

uris

array

List of selected file paths.

Sample code

media.pickFiles({
    success: function (data) {
        console.log(`handling success: ${data.uris}`)
    },
    fail: function (data, code) {
        console.log(`handling fail, code = ${code}`)
    }
})

[h2]media.pickImage(OBJECT)

Description

Selects an image.

OBJECT

Parameter

Type

Mandatory

Description

cancel

function

No

Callback upon cancellation.

success

function

No

Callback upon success.

fail

function

No

Callback upon failure.

complete

function

No

Callback upon completion.

Parameters returned in success

Parameter

Type

Description

uri

string

Path of the selected image.

name (1060+)

string

Name of the selected image.

size (1060+)

string

Size of the selected image, in bytes.

Sample code

media.pickImage({
    success:function(data){console.log("handling success: " + data.uri);}
})

[h2]media.pickImages(OBJECT) (1060+)

Description

Selects multiple images.

OBJECT

Parameter

Type

Mandatory

Description

success

function

No

Callback upon success.

fail

function

No

Callback upon failure.

cancel

function

No

Callback upon cancellation.

complete

function

No

Callback upon completion.

Parameters returned in success

Parameter

Type

Description

uris

array

List of selected image paths.

files

array

List of selected images. It is an array of file objects.

Sample code

media.pickImages({
    success: function (data) {
        console.log(`handling success: ${data.uris}`)
    },
    fail: function (data, code) {
        console.log(`handling fail, code = ${code}`)
    }
})

[h2]media.setRingtone(OBJECT) (1035+)

Description

Sets the ringtone.

Each time the API is called, an authorization pop-up will be displayed for users to give authorization.

This API does not support Vision products.

OBJECT

Parameter

Type

Mandatory

Description

title

string

No

Ringtone name. If this parameter is not set, it is set to the ringtone file name by default.

uri

string

Yes

Ringtone file path. Only a local path is supported.

CAUTION:

A local path is a project path or internal path (not of the tmp type).

type

string

Yes

Ringtone type.

  • ringtone
  • notification
  • alarm

success

function

No

Callback upon success.

fail

function

No

Callback upon failure.

complete

function

No

Callback upon completion.

Result codes returned in fail

Result Code

Description

202

Invalid parameter.

1001

The file does not exist.

Sample code

media.setRingtone({
    uri:"/Common/test.mp3",
    type:"ringtone",
    success: function () {
        console.log('media.setRingtone----------success')
    },
    fail: function (erromsg, errocode) {
        console.log('media.setRingtone----------' + errocode + ': ' + erromsg)
    },
    complete: function () {
        console.log('media.setRingtone----------complete')
    }
})

[h2]media.getRingtone(OBJECT) (1035+)

Description

Obtains the ringtone.

This API does not support Vision products.

OBJECT

Parameter

Type

Mandatory

Description

type

string

Yes

Ringtone type.

  • ringtone
  • notification
  • alarm

success

function

No

Callback upon success.

fail

function

No

Callback upon failure.

complete

function

No

Callback upon completion.

Parameters returned in success

Parameter

Type

Description

title

string

Ringtone name. If a ringtone is deleted, an empty string is returned. If a phone is muted, the none string is returned.

Result codes returned in fail

Result Code

Description

202

Invalid parameter.

If a user sets a parameter incorrectly, for example, a misspelt ringtone type, the API returns a result code indicating that the parameter is invalid.

In the case of dual SIM cards, if a ringtone is set for each SIM card, the ringtone of SIM card 1 is returned when requested.

Sample code

media.getRingtone({
    type:"alarm",
    success: function (ret) {
        console.log('media.getRingtone----------success'+ret.title)
    },
    fail: function (erromsg, errocode) {
        console.log('media.getRingtone----------' + errocode + ': ' + erromsg)
    },
    complete: function () {
        console.log('media.getRingtone----------complete')
    }
});

[h2]media.pickVideo(OBJECT)

Description

Selects a video.

OBJECT

Parameter

Type

Mandatory

Description

cancel

function

No

Callback upon cancellation.

success

function

No

Callback upon success.

fail

function

No

Callback upon failure.

complete

function

No

Callback upon completion.

Parameters returned in success

Parameter

Type

Description

uri

string

Path of the file.

name (1060+)

string

Name of the file.

size (1060+)

string

Size of the file, in bytes.

Sample code

media.pickVideo({
    success:function(data){console.log("handling success: " + data.uri);},
    fail: function(data, code) {
        console.log("handling fail, code=" + code);
    }
})

[h2]media.pickVideos(OBJECT) (1060+)

Description

Selects multiple videos.

OBJECT

Parameter

Type

Mandatory

Description

success

function

No

Callback upon success.

fail

function

No

Callback upon failure.

cancel

function

No

Callback upon cancellation.

complete

function

No

Callback upon completion.

Parameters returned in success

Parameter

Type

Description

uris

array

List of selected file paths.

files

array

List of selected files. It is an array of file objects.

Sample code

media.pickVideos({
    success: function (data) {
        console.log(`handling success: ${data.uris}`)
    },
    fail: function (data, code) {
        console.log(`handling fail, code = ${code}`)
    }
})

[h2]media.previewImage(OBJECT) (1035+)

Description

Previews an image. After the API is called, uploaded images are previewed in full-screen mode on a new page. Users can swipe left and right to preview these images, zoom in and out on the images using two fingers, and save the images to albums. This API does not support Vision products.

OBJECT

Parameter

Type

Mandatory

Description

current

string

No

Path of the image that is currently displayed.

uris

array

Yes

List of to-be-previewed image links. Both Internet and local addresses are supported.

success

function

No

Callback upon success.

fail

function

No

Callback upon failure.

complete

function

No

Callback upon completion.

Sample code

media.previewImage({
    current: "https://static.wgpet.com/editor/attached/image/20141124/20141124225933_71813.jpg",
    uris: ["https://img3.imgtn.bdimg.com/it/u=3993672553,267575750&fm=21&gp=0.jpg",
        "https://img5.imgtn.bdimg.com/it/u=2652905594,2769975769&fm=21&gp=0.jpg",
        "/common/logo.png",
            "https://imgsrc.baidu.com/forum/w%3D580/sign=fe8a7e7f78ec54e741ec1a1689399bfd/4b3f9682d158ccbfb334467b1ad8bc3eb33541cd.jpg",
        "https://static.wgpet.com/editor/attached/image/20141124/20141124225933_71813.jpg",
        "https://zyline-photo.qiniudn.com/1392705051156.jpg",
        "https://v1.qzone.cc/pic/201405/25/13/57/5381864e3b8af886.jpg%21600x600.jpg",
        "https://wanzao2.b0.upaiyun.com/system/pictures/23660313/original/8d4f08e50be02ae3.png"],
    success: function () {
        console.log('previewImage success');
    },
    fail: function (erromsg, errocode) {
        console.log('previewImage errocode:' + errocode);
    },
    complete: function () {
        console.log('previewImage complete');
    }
});

Demo

<template>
  <div class="container">
    <div class="item-container">
      <input type="button" class="btn" onclick="takePhoto" value="takePhoto" />
    </div>
    <div class="item-container">
      <input type="button" class="btn" onclick="takeVideo" value="takeVideo" />
    </div>
    <div class="item-container">
      <input type="button" class="btn" onclick="saveToPhotosAlbum" value="saveToPhotosAlbum" />
    </div>
    <div class="item-container">
      <input type="button" class="btn" onclick="pickFile" value="pickFile" />
    </div>
    <div class="item-container" if="{{pickFile_success || pickFile_fail}}">
      <text class="tip">{{pickFile_success}}</text>
      <text class="tip">{{pickFile_fail}}</text>
    </div>

    <div class="item-container">
      <input type="button" class="btn" onclick="pickFiles" value="pickFiles" />
    </div>
    <div class="item-container" if="{{pickFiles_success || pickFiles_fail}}">
      <text class="tip">{{pickFiles_success}}</text>
      <text class="tip">{{pickFiles_fail}}</text>
    </div>
    <div class="item-container">
      <input type="button" class="btn" onclick="pickImage" value="pickImage" />
    </div>
    <div if="{{pickImage_success}}">
      <text class="tip">{{pickImage_success}}</text>
    </div>
    <div class="item-container">
      <input type="button" class="btn" onclick="pickImages" value="pickImages" />
    </div>
    <div class="item-container" if="{{pickImages_success || pickImages_fail}}">
      <text class="tip">{{pickImages_success}}</text>
      <text class="tip">{{pickImages_fail}}</text>
    </div>
    <div class="item-container">
      <input type="button" class="btn" onclick="setRingtone" value="setRingtone" />
    </div>
    <div class="item-container" if="{{setRingtone_success || setRingtone_fail}}">
      <text class="tip">{{setRingtone_success}}</text>
      <text class="tip">{{psetRingtone_fail}}</text>
    </div>
    <div class="item-container">
      <input type="button" class="btn" onclick="getRingtone" value="getRingtone" />
    </div>
    <div class="item-container" if="{{getRingtone_success || getRingtone_fail}}">
      <text class="tip">{{getRingtone_success}}</text>
      <text class="tip">{{gsetRingtone_fail}}</text>
    </div>
    <div class="item-container">
      <input type="button" class="btn" onclick="pickVideo" value="pickVideo" />
    </div>
    <div class="item-container" if="{{pickVideo_success || pickVideo_fail}}">
      <text class="tip">{{pickVideo_success}}</text>
      <text class="tip">{{pickVideo_fail}}</text>
    </div>
    <div class="item-container">
      <input type="button" class="btn" onclick="pickVideo" value="pickVideo" />
    </div>
    <div class="item-container" if="{{pickVideos_success || pickVideos_fail}}">
      <text class="tip">{{pickVideos_success}}</text>
      <text class="tip">{{pickVideos_fail}}</text>
    </div>
    <div  class="item-container">
      <input type="button" class="btn" onclick="previewImage" value="previewImage" />
    </div>
    <div class="item-container" if="{{previewImage_fail}}">
      <text class="tip">{{previewImage_fail}}</text>
    </div>
  </div>
</template>

<style>
    .container {
      flex: 1;
      background-color: #F1F3F5;
      flex-direction: column;
    }
    .page-title-wrap {
      padding-top: 50px;
      padding-bottom: 80px;
      justify-content: center;
    }
    .tip {
      border: 1px solid #000000;
      text-align: center;
    }
    .page-title {
      padding-top: 30px;
      padding-bottom: 30px;
      padding-left: 40px;
      padding-right: 40px;
      border-color: #bbbbbb;
      color: #bbbbbb;
      border-bottom-width: 2px;
    }
    .item-container {
      margin-left: 32px;
      margin-right: 32px;
      padding-top: 20px;
      padding-right: 20px;
      padding-bottom: 20px;
      padding-left: 20px;
      background-color: #F1F3F5;
      border-radius: 32px;
      flex-direction: column;
    }
    .btn {
      height: 80px;
      text-align: center;
      border-radius: 5px;
      margin-right: 60px;
      margin-left: 60px;
      color: #ffffff;
      font-size: 30px;
      background-color: #0faeff;
    }
</style>

<script>

    import media from '@system.media'
    export default {
        data: {
          hello:"123124",
            checked: "checked",
            pickFile_success: "",
            pickFile_fail: "",
            pickFiles_success: "",
            pickFiles_fail: "",
            pickImage_success: "",
            pickImages_success: "",
            pickImages_fail: "",
            setRingtone_success: "",
            setRingtone_fail: "",
            getRingtone_success: "",
            getRingtone_fail: "",
            pickVideo_success:"",
            pickVideo_fail:"",
            pickVideos_success:"",
            pickVideos_fail:"",
            previewImage_fail:""
        },
        onInit() {
            this.$page.setTitleBar({ text: 'Media' })
        },
        takePhoto() {
            media.takePhoto({
                success: function (data) {
                    console.log(data.uri + "handling success: ");
                },
                fail: function (data, code) {
                    console.log("handling fail, code=" + code);
                }
            });
        },
        takeVideo() {
            media.takeVideo({
                maxDuration: 10,
                success: function (data) { console.log("handling success: " + data.uri); },
                fail: function (data, code) {
                    console.log("handling fail, code=" + code);
                }
            })
        },
        saveToPhotosAlbum() {
            media.saveToPhotosAlbum({
                uri: 'internal://mass/Img/Img_20220208_095117.jpg',
                folderName: 'custom-folder',
                success: function (data) { console.log("save success: "); },
                fail: function (data, code) {
                    console.log("handling fail, code=" + code);
                }
            })
        },
        pickFile() {
            var that = this
            media.pickFile({
                success: function (data) {
                    console.log("handling success: " + data.uri)
                    that.pickFile_success = data.uri

                },
                fail: function (data, code) {
                    console.log("handling fail, code=" + code)
                    that.pickFile_fail = code
                }
            })
        },
        pickFiles() {
            var that = this
            media.pickFiles({
                success: function (data) {
                    console.log(`handling success: ${data.uris}`)
                    that.pickFiles_success = `${data.uris}`
                },
                fail: function (data, code) {
                    console.log(`handling fail, code = ${code}`)
                    that.pickFiles_fail = `${code}`
                }
            })
        },
        pickImage() {
            var that = this
            media.pickImage({
                success: function (data) {
                    console.log("handling success: " + data.uri);
                    that.pickImage_success = data.uri
                }

            })
        },
        pickImages() {
            var that = this
            media.pickImages({
                success: function (data) {
                    console.log(`handling success: ${data.uris}`)
                    that.pickImages_success = `${data.uris}`
                },
                fail: function (data, code) {
                    console.log(`handling fail, code = ${code}`)
                    that.pickImages_fail = `${code}`
                }
            })
        },
        setRingtone() {
            var that = this
            media.setRingtone({
                uri: "/Common/test.mp3",
                type: "ringtone",
                success: function () {
                    console.log('media.setRingtone----------success')
                    that.setRingtone_success = "media.setRingtone----------success"
                },
                fail: function (erromsg, errocode) {
                    console.log('media.setRingtone----------' + errocode + ': ' + erromsg)
                    that.setRingtone_fail = errocode + ': ' + erromsg
                },
                complete: function () {
                    console.log('media.setRingtone----------complete')
                }
            })
        },
        getRingtone() {
            var that = this
            media.getRingtone({
                type: "alarm",
                success: function (ret) {
                    console.log('media.getRingtone----------success' + ret.title)
                    that.getRingtone_success = "ret.title"
                },
                fail: function (erromsg, errocode) {
                    console.log('media.getRingtone----------' + errocode + ': ' + erromsg)
                    that.getRingtone_fail = errocode + ': ' + erromsg
                },
                complete: function () {
                    console.log('media.getRingtone----------complete')
                }
            });
        },
        pickVideo() {
            var that = this
            media.pickVideo({
                success: function (data) { 
                    console.log("handling success: " + data.uri); 
                    that.pickVideo_success = data.uri
                },
                fail: function (data, code) {
                    console.log("handling fail, code=" + code);
                    that.pickVideo_success = code
                }
            })

        },
        pickVideos() {
            var that = this
            media.pickVideos({
                success: function (data) {
                    console.log(`handling success: ${data.uris}`)
                      that.pickVideos_success = ` ${data.uris}`
                },
                fail: function (data, code) {
                    console.log(`handling fail, code = ${code}`)
                    that.pickVideos_success = ` ${code}`
                }
            })

        },
        previewImage() {
            var that= this
            media.previewImage({
                current: "https://static.wgpet.com/editor/attached/image/20141124/20141124225933_71813.jpg",
                uris: ["https://img3.imgtn.bdimg.com/it/u=3993672553,267575750&fm=21&gp=0.jpg",
                    "https://img5.imgtn.bdimg.com/it/u=2652905594,2769975769&fm=21&gp=0.jpg",
                    "/common/logo.png",
                    "https://imgsrc.baidu.com/forum/w%3D580/sign=fe8a7e7f78ec54e741ec1a1689399bfd/4b3f9682d158ccbfb334467b1ad8bc3eb33541cd.jpg",
                    "https://static.wgpet.com/editor/attached/image/20141124/20141124225933_71813.jpg",
                    "https://zyline-photo.qiniudn.com/1392705051156.jpg",
                    "https://v1.qzone.cc/pic/201405/25/13/57/5381864e3b8af886.jpg%21600x600.jpg",
                    "https://wanzao2.b0.upaiyun.com/system/pictures/23660313/original/8d4f08e50be02ae3.png"],
                success: function () {
                    console.log('previewImage success');
                },
                fail: function (erromsg, errocode) {
                    console.log('previewImage errocode:' + errocode + ': ' + erromsg);
                    that.previewImage = errocode + ': ' + erromsg
                },
                complete: function () {
                    console.log('previewImage complete');
                }
            });
        }
    }
</script>

The following figure shows the final effect.

Version Change History

Version

Date

Description

1080

2021-12-28

Added the folderName parameter to the saveToPhotosAlbum method.

1060

2019-12-10

  • Added the return parameters name and size for the takePhoto, takeVideo, pickImage, pickVideo, and pickFile methods to obtain the file name and size respectively.
  • Added the pickImages, pickVideos, and pickFiles methods.

1040

2019-06-24

  • Added the setRingtone and getRingtone methods to set ringtones.
  • Added the previewImage method to preview images.

1010

2018-04-20

Added the saveToPhotosAlbum and pickFile methods.

Links

[h2]FAQs

What can I do if my Huawei phone displays an error message "code=200, data generic error" and the ringtone I set for a quick app does not take effect?

[h2]Cases

Error Code 202 Is Returned When the media.saveToPhotosAlbum API Is Called