tencent cloud

Quick Start
Last updated:2026-01-07 11:03:07
Quick Start
Last updated: 2026-01-07 11:03:07

Preparations

1. You need an Android application, which can be your existing project or a new empty project.
2. Ensure your Android application targets API level 21 (Android 5.0) or higher.
3. You need a business server API that can obtain an access token for Smart Media Hosting (SMH). For descriptions of the access token, please refer to Generate Access Token.

Step 1: Installing the SDK

Automatic Integration

Using mavenCentral Repository

Add the following to the build.gradle file at the project level (usually in the root directory):
repositories {
google()
// add this line
mavenCentral()
}
Add dependencies to the build.gradle file at the app-level (usually in the app module):
dependencies {
...
// add this line
implementation 'com.qcloud.cos:smh-android:1.2.37'
}

Disabling the beacon Reporting Function

To continuously track and optimize the quality of the SDK, bringing you a better user experience, we have introduced the Tencent Beacon SDK.
Note
Tencent Beacon only monitors the request performance on the SMH side and does not report business-side data.
If you want to disable this feature, modify the dependency of smh-android in the build.gradle file at the app-level (usually in the app module) to:
dependencies {
...
change to
implementation 'com.qcloud.cos:smh-android-nobeacon:1.2.37'
}

Step 2: Configuring Permissions

Network Permission

The SDK requires network permission to communicate with the SMH server. Please add the following permission statement in the AndroidManifest.xml file under the application module:
<uses-permission android:name="android.permission.INTERNET"/>

Storage Permission

If your application needs to read/write files from external storage, add the following permission statement in the AndroidManifest.xml file under the application module:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Note:
On Android 6.0 (API level 23) and above, you need to dynamically request storage permissions at runtime.

Step 3: Get Started

Implementing Access Token Retrieval

Implement a subclass of SMHSimpleUser to retrieve the libraryId and userSpace, request an access token, and return the result.
class MySMHSimpleUser: SMHSimpleUser() {
override val libraryId: String
get() = "smh3ptyc9mscifdi"
override val userSpace: UserSpace
get() = UserSpace(
userId = "7",
spaceId = "space1x8mfjgno6nyy"
)

override suspend fun provideAccessToken(): AccessToken {
// First, get the response containing access token information from your access token server

// Then parse the response to get the access token info
String token = "token"; // Access token
long expiresIn = 86400;// access token valid duration, in seconds

// Recommend using server time as the signature start time to avoid request expiration due to excessive deviation in user's mobile local time
// Use server time as the signature start time
long startTime = 1556182000L; // access token start timestamp, in ms

// Finally return the access token info object
return new AccessToken(token, startTime, expiresIn);
}
}
Here suppose the class is named MySMHSimpleUser. Initialize an instance to provide the access token to the SDK.
val mySMHSimpleUser: MySMHSimpleUser = MySMHSimpleUser();

Initializing SMHCollection

Use the instance mySMHSimpleUser with your provided key to initialize an instance of SMHCollection.
SMHCollection provides access to all APIs for SMH. In follow-up documentation, SMH refers to the created SMHCollection instance.
Note:
After a media library is created in the console, the console will display a dedicated domain name generated for you. It is strongly recommended to set customHost to your dedicated domain name when creating an SMHCollection instance.

Initialize SMHCollection, get instance
val smh: SMHCollection = SMHCollection(
context = context,
user = mySMHSimpleUser,
// Assign the "Access domain" displayed after creating a media library through the console to customHost
customHost = "<libraryId>.api.tencentsmh.cn"
)

Step 4: Access the SMH Service

For example: File list
//target folder
val targetDir = Directory()
try {
val directoryContents: DirectoryContents = smh.list(
//target directory
dir = targetDir,
//page number
page = 1,
//Items per Page
pageSize = 100,
//Sorting method
orderType = OrderType.NAME,
//Sorting order
orderDirection = OrderDirection.ASC,
//Filter type
directoryFilter = DirectoryFilter.ONLY_FILE
)
//File/directory list. Check the DirectoryContents entity for other numbers etc.
val contents = directoryContents.contents
} catch (e: Exception) {
e.printStackTrace()
}

General Parameter Introduction

LibraryId: Media Library ID, required parameter.
SpaceId: Space ID. If the media library is in single-tenant mode, this parameter is fixed as a hyphen (-). If the media library is in multi-tenant mode, you must specify this parameter.
AccessToken: Access token, required parameter.
UserId: User identity recognition. When the access token corresponds to admin permission and the user identity recognition during token application is empty, it is used to temporarily designate user identity. For details, see Generate Access Token, optional parameter.
OrganizationId: Organization ID, required parameter.
UserToken: User token, required parameter.
Note
For more concepts, see Basic Concepts.
Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback