All Products
Search
Document Center

Drive and Photo Service:Android SDK overview

Last Updated:Dec 23, 2025

Integration guide

SDK name

Drive and Photo Service SDK for Android

Developer

Alibaba Cloud Computing Co., Ltd.

SDK version

0.1.4

SDK package name

com.aliyun.pds.sdk

SDK update time

2023-07-19

SDK size

159.76 KB

SDK MD5 hash

47643c218b682f5c59ac2f17bb9044b4

Privacy policy

Drive and Photo Service SDK Privacy Policy

SDK download

aliyun-pds-sdk-0.1.4.aar

Integrate the SDK

Import the SDK using Gradle.

implementation 'com.aliyun.pds:android-sdk:0.1.4'

Source code: aliyun-pds-android-sdk.

Important

The minimum supported Android SDK version is API 21.

Initialization

Before using the software development kit (SDK), make sure you have activated Drive and Photo Service and created an instance in the console. For more information, see Get started with PDS.

// Obtain the access token from your account system. Your backend uses the AppKey and AppSecret from the PDS platform to request a token and returns it to the client.
val token = SDToken("your access token") 
// Get your API host from the PDS console.
val apiHost = "your API host"            
val config = SDConfig.Builder(token, apiHost, 3600)
    // Specifies whether to enable instant file transfer. Default: true. (Optional)
    .canFastUpload() 
    // The User-Agent for the request. (Optional)
    .userAgent() 
    // The maximum number of retries. Default: 3. (Optional)
    .maxRetryCount()
    // Specifies whether to enable debug mode. Default: false. (Optional)
    .isDebug()
    // The size of a download shard. Default: 10 MB. Do not set this value too small or too large. (Optional)
    .downloadBlockSize() 
    // The size of an upload shard. Default: 4 MB. Do not set this value too small or too large. (Optional)
    .uploadBlockSize() 
    // The timeout period for establishing a network connection. Default: 15s. (Optional)
    .connectTimeout()
    // The timeout period for a network connection response. Default: 60s. (Optional)
    .readTimeout()
    // The timeout period for a network transmission response. Default: 60s. (Optional)
    .writeTimeout()         
    .build()
SDClient.instance.init(this, config)
Note

Note that the SDK does not automatically refresh the access token. You must periodically retrieve the latest token from your server and pass it to the SDK. Use the following code to update the token.

SDClient.instance.updateToken(token)

Upload and download files

Note

Note that task progress and status callbacks run in a subthread. To update the UI, switch to the main thread.

Download tasks

// Initialize download information.
val downloadInfo = DownloadRequestInfo.Builder()
    .downloadUrl(url)
    .driveId(driveId)
    .fileId(fileId)
    .fileName(fileName)
    .fileSize(fileSize)
    // The path to save the file.
    .filePath(dir.path)
    // From a share: ID. This parameter is optional if the file is not from a share.
    .shareId(shareId)
    // From a share: token. This parameter is optional if the file is not from a share.
    .shareToken(shareToken) 
    // From a share: password. This parameter is optional if the file is not from a share.
    .sharePwd(sharePwd)
    // For historical versions: ID. This parameter is required when downloading a historical version of a file. It is optional otherwise.
    .revisionId(revisionId)
    // The hash value for verification.
    .contentHash(hash)
    // The hash verification algorithm. Currently, only crc64 is supported.
    .contentHashName("crc64")       
    .build()

/* 
* Create and start a task.
* If you specify the taskId of an incomplete task, the download resumes from where it left off.
* Do not specify the taskId of a completed task. This will immediately trigger the completion callback.
* Use a unique taskId for each new task. Reuse a taskId only to resume a task.
*/
val task = SDClient.instance.startDownloadTask(
    taskId,                 
    downloadInfo,
    // The listener for download completion. This callback is triggered on success or failure. An error message is returned on failure.
    completeListener,
    // The listener for download progress.
    progressListener        	
)

/*
* By default, startDownloadTask starts the task.
* If a task is stopped, call this method to restart and continue it.
*/
task.start()

/* 
* Stop the task. If the clean parameter is false, temporary files are not deleted, and calling start() resumes the task.
* If the clean parameter is true, temporary files and data are deleted, and calling start() restarts the task from the beginning.
*/
task.stop(clean)

Upload tasks

// Initialize upload information.
val uploadInfo = UploadRequestInfo.Builder()
    .fileName("edmDrive")
    .filePath(file.absolutePath)
    .fileSize(file.length())
    .parentId(parentId)
    .driveId(driveId)
    .mimeType(mimeType)
    // Specify the file ID. This parameter is required to overwrite a file.
    .fileId(fileId)
    /* 
    * How to handle a file with a duplicate name. Default: "auto_rename". The parameters are described as follows:
    *  auto_rename: If a file with the same name exists, the cloud service automatically renames it. By default, the current timestamp is appended, such as xxx_20060102_150405.
    *  ignore: Allows files with the same name.
    *  refuse: If a file with the same name exists, the service rejects the new file and returns a success message.
    */
    .checkNameMode(checkNameMode)       
    // For shares. These parameters are optional if not related to a share.                                
    .shareId(shareId)                   
    .shareToken(shareToken)             
    .sharePwd(sharePwd)                 
    .build()

// Create and start the task.
val task = SDClient.instance.startUploadTask(
    taskId,                 
    uploadInfo,
    // The listener for upload completion. This callback is triggered on success or failure. An error message is returned on failure.
    completeListener,
    // The listener for upload progress.
    progressListener        
)

/* 
* By default, startUploadTask starts the task.
* If a task is stopped, call this method to restart and continue it.
*/
task.start()

/*
* Stop the task. If the clean parameter is false, temporary files are not deleted, and calling start() resumes the task.
* If the clean parameter is true, temporary files and data are deleted, and calling start() restarts the task from the beginning.
*/
task.stop(clean)

Task progress callback

// currentSize is the current progress. Note that the callback is not on the main thread.
interface OnProgressListener {
    fun onProgressChange(currentSize : Long)
}

Task completion callback

interface OnCompleteListener {
    fun onComplete(taskId: String, fileMeta : SDFileMeta, errorInfo: SDErrorInfo?)
}

The fileMeta parameter contains file information.

class SDFileMeta(
    // The file ID.
    val fileId: String?,
    // The file name.
    val fileName: String?,
    // The file path. For an upload task, this is the path of the file to upload. For a download task, this is the path where the file is saved.
    val filePath: String?,
    // The uploadId for the upload task. You can ignore this if it is not relevant to your business.
    val uploadId: String? = "" 
)

class SDErrorInfo(
    // The error code.
    val code: SDTransferError,
    // The error description.
    val message: String,
    // The exception, used to view the stack and handle errors.
    val exception: Exception?,
    // If a backend request error occurs, this value is returned to help with troubleshooting.
    var requestId: String? = "" 
)

Error codes

SDTransferError.Unknown // Unknown error.
SDTransferError.Network // Network error.
SDTransferError.Server // Server error.
SDTransferError.FileNotExist // During upload, the local file was not found.
SDTransferError.SpaceNotEnough // During download, there is not enough local storage space.
SDTransferError.TmpFileNotExist // During download, the local temporary file does not exist.
SDTransferError.PathRuleError // During download, the save path rule is invalid.

After a task is created, it starts automatically. You can retrieve the current task progress from the progress callback. The completion listener is called when the task succeeds or fails.

File API operations

For more information about request and response parameters, see the File management API reference.

Retrieve the fileApi object from SDClient.fileApi, and then call the following methods to access the corresponding APIs.

Note

Note: The parameters in the request examples are basic parameters. For other parameters, see the official API reference.

List files or folders

fun fileList(fileListRequest: FileListRequest): FileListResp?

// For other FileListRequest parameters, see the FileListRequest implementation.
val request = FileListRequest()
// The fileId of the folder to list. Use "root" for the root directory.
request.parentId = "root"
// The driveId that contains the list.
request.driveId = ""                    

Create a file or folder

// FileCreateRequest example.
val createRequest = FileCreateRequest()
// enum (ignore, auto_rename, refuse)
createRequest.checkNameMode = "auto_rename"
// The driveId where the file will be created.
createRequest.driveId = "" 
// The name of the new file.
createRequest.name = ""
// The fileId of the parent directory. Use "root" for the root directory.
createRequest.parentFileId = "root"
// enum (file, folder)
createRequest.type = "folder"                   

Get a file or folder

// Get file or folder information.
fun fileGet(getResp: FileGetRequest): FileGetResp?

// FileGetRequest example.
val getRequest = FileGetRequest()
// The driveId of the file.
getRequest.driveId = "" 
// The fileId.
getRequest.fileId = ""                      

Copy a file or folder

// Copy a file or folder.
fun fileCopy(fileCopyRequest: FileCopyRequest): FileCopyResp?

// FileCopyRequest example.
val copyRequest = FileCopyRequest()
// The driveId of the file.
copyRequest.driveId = ""  
// The fileId.
copyRequest.fileId = ""
// The driveId of the destination folder.
copyRequest.toDriveId = ""
// The new name for the copied file.
copyRequest.newName = ""
// The fileId of the destination parent folder. Use "root" for the root directory.
copyRequest.toParentId = "root"         

Move a file or folder

// Move a file or folder.
fun fileMove(fileMoveRequest: FileMoveRequest): FileMoveResp?

// FileMoveRequest example.
val moveRequest = FileMoveRequest() 
// The driveId of the file.
moveRequest.driveId = "" 
// The fileId.
moveRequest.fileId = ""
// The driveId of the destination folder.
moveRequest.toDriveId = ""
// The new name for the moved file.
moveRequest.newName = "" 
// The fileId of the destination parent directory. Use "root" for the root directory.
moveRequest.toParentId = "root"             

Update a file or folder

fun fileUpdate(updateRequest: FileUpdateRequest): FileGetResp?

// FileUpdateRequest example.
val updateRequest = FileUpdateRequest()
// The driveId of the file.
updateRequest.driveId = item.driveId!!
// The fileId.
updateRequest.fileId = item.fileId 
// The new name for the updated file.
updateRequest.name = ""                     

Delete a file or folder

fun fileDelete(deleteRequest: FileDeleteRequest): FileDeleteResp?

// FileDeleteRequest example.
val delRequest = FileDeleteRequest()
// The driveId of the file.
delRequest.driveId = ""
// The fileId of the file or folder to delete.
delRequest.fileId = ""                      

Search for files

fun fileSearch(fileSearchRequest: FileSearchRequest): FileListResp?

// FileSearchRequest example.  
// For query rules, see https://www.alibabacloud.com/help/document_detail/175890.html
val request = FileSearchRequest()
// keyStr: The search keyword.
request.query = "name match '$keyStr' and status = 'available'"     
request.driveId = ""  

Other operations

// Get the upload URL for file shards.
fun fileGetUploadUrl(getUploadUrlRequest: FileGetUploadUrlRequest): FileGetUploadUrlResp?

// Complete the file upload.
fun fileComplete(completeRequest: FileCompleteRequest): FileGetResp?

// Get the file download URL.
fun fileGetDownloadUrl(getDownloadUrlRequest: FileGetDownloadUrlRequest): FileGetDownloadUrlResp?

// Get the status of an asynchronous task. For example, deleting a folder with multiple files is an asynchronous task. You can use this interface to get the task status.
fun getAsyncTask(getAsyncTaskRequest: AsyncTaskRequest): AsyncTaskResp?