tencent cloud

Feedback

Upload SDK for Android

Last updated: 2024-11-15 10:52:44
    Upload VOD provides an SDK for uploading videos from Android clients. For details about the upload process, see Guide.
    SDK Name
    Vod Upload SDK For Android
    Version
    V1.1.23.0
    SDK Introduce
    Providing a scenario for end-users of an app to upload local videos to a cloud video on demand platform:
    Developer
    Tencent Cloud Computing (Beijing) Co., Ltd.
    Download SDK
    1. Click to download the iOS upload Demo and source code, unzip the downloaded package, and you can see the Demo directory.
    2. Upload the source code in the Demo/app/src/main/java/com/tencent/ugcupload/demo/videoupload directory.

    Integrating the Source code and Libraries

    1. Copy the source code directory Demo/app/src/main/java/com/tencent/ugcupload/demo/videoupload into your project directory, and you need to manually modify the package name.
    2. Refer to Demo/app/build.gradle to add dependencies in your project:
    implementation 'com.qcloud.cos:cos-android-nobeacon:5.9.25'
    implementation 'com.qcloud.cos:quic:1.5.43'
    Note:
    You can also refer to the manual integration documentation to integrate the corresponding version of the dependency library.
    3. To use video uploading, you need network and storage access permissions. You can add the following permission declarations in the AndroidManifest.xml file:
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    4. Video uploading requires refreshing the upload IP based on network changes. You can dynamically register broadcasts according to your business needs, as shown in the following example:
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // your code......
    registerNetReceiver();
    }
    
    private void registerNetReceiver() {
    if (null == mNetWorkStateReceiver) {
    mNetWorkStateReceiver = new TVCNetWorkStateReceiver();
    IntentFilter intentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
    registerReceiver(mNetWorkStateReceiver, intentFilter);
    }
    }
    
    private void unRegisterNetReceiver() {
    if (null != mNetWorkStateReceiver) {
    unregisterReceiver(mNetWorkStateReceiver);
    }
    }
    
    @Override
    protected void onDestroy() {
    super.onDestroy();
    // your code......
    unRegisterNetReceiver();
    }

    Simple video uploading

    Initializing the upload object

    TXUGCPublish mVideoPublish = new TXUGCPublish(this.getApplicationContext(), "independence_android")

    Setting upload object callbacks

    mVideoPublish.setListener(new TXUGCPublishTypeDef.ITXVideoPublishListener() {
    @Override
    public void onPublishProgress(long uploadBytes, long totalBytes) {
    mProgress.setProgress((int) (100*uploadBytes/totalBytes));
    }
    
    @Override
    public void onPublishComplete(TXUGCPublishTypeDef.TXPublishResult result) {
    mResultMsg.setText(result.retCode + " Msg:" + (result.retCode == 0 ? result.videoURL : result.descMsg));
    }
    });

    Constructing upload parameters

    TXUGCPublishTypeDef.TXPublishParam param = new TXUGCPublishTypeDef.TXPublishParam();
    
    param.signature = "xxx";
    param.videoPath = "xxx";
    Signature calculation rules, please refer to Client-side Upload Signature.

    Call upload

    int publishCode = mVideoPublish.publishVideo(param);

    Simple image upload

    Initialize the upload object

    TXUGCPublish mVideoPublish = new TXUGCPublish(this.getApplicationContext(), "independence_android")

    Set upload object callbacks

    mVideoPublish.setListener(new TXUGCPublishTypeDef.ITXMediaPublishListener() {
    @Override
    public void onMediaPublishProgress(long uploadBytes, long totalBytes) {
    mProgress.setProgress((int) (100*uploadBytes/totalBytes));
    }
    @Override
    public void onMediaPublishComplete(TXUGCPublishTypeDef.TXMediaPublishResult mediaResult) {
    mResultMsg.setText(result.retCode + " Msg:" + (result.retCode == 0 ? result.videoURL : result.descMsg));
    }
    });

    Construct upload parameters

    TXUGCPublishTypeDef.TXMediaPublishParam param = new TXUGCPublishTypeDef.TXMediaPublishParam();
    
    param.signature = "xxx";
    param.mediaPath = "xxx";
    Signature calculation rules, please refer to Client-side Upload Signature.

    Call upload

    int publishCode = mVideoPublish.publishMedia(param);
    Note:
    The upload method automatically chooses normal upload or slice upload based on the length of the user's file, and the user does not need to worry about the steps of slice upload, which can realize slice upload.
    If you need to upload to a specific application, please refer Application System - Client-side Upload

    Advanced Features

    With cover

    Just include the cover path
    TXUGCPublishTypeDef.TXPublishParam param = new TXUGCPublishTypeDef.TXPublishParam();
    param.signature = "xxx";
    param.videoPath = "xxx";
    param.coverPath = "xxx";
    Signature calculation rules, please refer to Client-side Upload Signature.

    Cancel and Resume Upload

    Cancel the upload by calling the cancelublish() interface of TXUGCPublish.
    mVideoPublish.canclePublish();
    Resume the upload by using the same upload parameters (the video path and cover image path remain unchanged) and call the publishVideo method of TXUGCPublish again.

    Breakpoint resume

    During the video upload process, cloud video on demand supports breakpoint resume, which means that if the upload is accidentally terminated, users can continue uploading the file from where it was interrupted, reducing the time spent on re-uploading. The valid time for breakpoint resume is 1 day, meaning that if the same video upload is interrupted, it can be directly uploaded from the breakpoint within 1 day. After 1 day, it will default to re-uploading the entire video.
    The enableResume parameter in the upload settings is the switch for breakpoint resume, which is enabled by default.

    Pre-upload

    In actual upload processes, a large portion of errors are caused by network connection failures or timeouts. To optimize such issues, we have added pre-upload optimization logic. Pre-upload includes: HTTPDNS resolution, obtaining suggested upload regions, and detecting the optimal upload region.
    We recommend that you call TXUGCPublishOptCenter.getInstance().prepareUpload(signature) when your app starts. The pre-upload module will cache the<domain name, IP> mapping table and the optimal upload region locally. If network broadcasts have been dynamically registered previously, upon subscription to network switches, the cache will be cleared and automatically refreshed.
    Signature calculation rules, please refer to Client-side Upload Signature.

    Enabling HTTPS Upload

    To enable HTTPS upload, simply set the enableHTTPS parameter in TXPublishParam to true. By default, it is set to false.
    TXUGCPublishTypeDef.TXPublishParam param = new TXUGCPublishTypeDef.TXPublishParam();
    param.enableHttps = true;

    Disable logging

    To disable logging, you need to operate through the setIsDebug method of TXUGCPublish. By default, it is enabled. When enabled, it will print logcat logs and also save the logs to the app's private directory.
    // false Disable logging
    mTXUGCPublish.setIsDebug(false);

    Video Upload Interface Description

    Initialize the upload object:TXUGCPublish
    Parameter Name
    Parameter Description
    Type
    Required
    context
    application cobtext
    Context
    YES
    customKey
    Used to distinguish different users, it is recommended to use the App's account ID for easier subsequent issue tracking.
    String
    NO
    Set the Vod app ID:TXUGCPublish.setAppId
    Parameter Name
    Parameter Description
    Type
    Required
    appId
    vod appId。
    int
    YES
    upload video:TXUGCPublish.publishVideo
    Parameter Name
    Parameter Description
    Type
    Required
    param
    upload params.
    TXUGCPublishTypeDef.TXPublishParam
    YES
    upload params:TXUGCPublishTypeDef.TXPublishParam
    Parameter Name
    Parameter Description
    Type
    Required
    signature
    String
    YES
    videoPath
    Local video file path.
    String
    YES
    coverPath
    Local cover file path, default without cover file.
    String
    NO
    enableResume
    Whether to enable resuming from the breakpoint, default is enabled.
    boolean
    NO
    enableHttps
    Whether to enable HTTPS, default is disabled.
    boolean
    NO
    fileName
    The name of the video file uploaded to Tencent Cloud, if not filled, the default is the local file name.
    String
    NO
    enablePreparePublish
    Whether to enable the pre-upload mechanism, default is enabled. The pre-upload mechanism can significantly improve the upload quality of files
    boolean
    NO
    sliceSize
    Chunk size, supports a minimum of 1MB and a maximum of 10MB, default is the uploaded file size divided by 10
    long
    NO
    concurrentCount
    The maximum number of concurrent uploads for chunked uploads, default is 4.
    int
    NO
    trafficLimit
    The speed limit value setting range is 819200 ~ 838860800, that is, 100KB/s ~ 100MB/s. If it exceeds this range, a 400 error will be returned. It is not recommended to set this value too small to prevent timeouts. -1 indicates no speed limit.
    long
    NO
    uploadResumeController
    The resume controller, which can be customized to calculate and save the resume key values, defaults to using MD5 to calculate the file key values.
    IUploadResumeController
    NO
    set upload callback:TXUGCPublish.setListener
    Parameter Name
    Parameter Description
    Type
    Required
    listener
    Upload progress and result callback subscription.
    TXUGCPublishTypeDef.ITXVideoPublishListener
    YES
    progress callback:TXUGCPublishTypeDef.ITXVideoPublishListener.onPublishProgress
    Parameter Name
    Parameter Description
    Type
    uploadBytes
    The number of bytes uploaded.
    long
    totalBytes
    Total number of bytes.
    long
    result callback:TXUGCPublishTypeDef.ITXVideoPublishListener.onPublishComplete
    Parameter Name
    Parameter Description
    Type
    result
    upload result
    TXUGCPublishTypeDef.TXPublishResult
    upload result:TXUGCPublishTypeDef.TXPublishResult
    Parameter Name
    Parameter Description
    Type
    retCode
    result code
    int
    descMsg
    Error description for upload failure.
    String
    videoId
    Vod video file ID.
    String
    videoURL
    Video storage address.
    String
    coverURL
    Cover storage address.
    String
    Pre-upload:TXUGCPublishOptCenter.prepareUpload
    Parameter Name
    Parameter Description
    Type
    Required
    signature
    String
    YES

    Image upload interface description

    Initialize upload object:TXUGCPublish
    Parameter Name
    Parameter Description
    Type
    Required
    context
    application cobtext
    Context
    YES
    customKey
    Used to distinguish different users, it is recommended to use the App's account ID for easier subsequent issue tracking.
    String
    NO
    Set the Vod app ID:TXUGCPublish.setAppId
    Parameter Name
    Parameter Description
    Type
    Required
    appId
    vod appId。
    int
    YES
    upload image:TXUGCPublish.publishMedia
    Parameter Name
    Parameter Description
    Type
    Required
    param
    upload params.
    TXUGCPublishTypeDef.TXMediaPublishParam
    YES
    upload params:TXUGCPublishTypeDef.TXMediaPublishParam
    Parameter Name
    Parameter Description
    Type
    Required
    signature
    String
    YES
    mediaPath
    Local image file path.
    String
    YES
    enableResume
    Whether to enable resuming from the breakpoint, default is enabled.
    boolean
    NO
    enableHttps
    Whether to enable HTTPS, default is disabled.
    boolean
    NO
    fileName
    The name of the video file uploaded to Tencent Cloud, if not filled, the default is the local file name.
    String
    NO
    enablePreparePublish
    Whether to enable the pre-upload mechanism, default is enabled. The pre-upload mechanism can significantly improve the upload quality of files
    boolean
    NO
    sliceSize
    Chunk size, supports a minimum of 1MB and a maximum of 10MB, default is the uploaded file size divided by 10
    long
    NO
    concurrentCount
    The maximum number of concurrent uploads for chunked uploads, default is 4.
    int
    NO
    trafficLimit
    The speed limit value setting range is 819200 ~ 838860800, that is, 100KB/s ~ 100MB/s. If it exceeds this range, a 400 error will be returned. It is not recommended to set this value too small to prevent timeouts. -1 indicates no speed limit.
    long
    NO
    uploadResumeController
    The resume controller, which can be customized to calculate and save the resume key values, defaults to using MD5 to calculate the file key values.
    IUploadResumeController
    NO
    set upload callback:TXUGCPublish.setListener
    Parameter Name
    Parameter Description
    Type
    Required
    listener
    Upload progress and result callback subscription.
    TXUGCPublishTypeDef.ITXMediaPublishListener
    YES
    progress callback:TXUGCPublishTypeDef.ITXMediaPublishListener.onPublishProgress
    Parameter Name
    Parameter Description
    Type
    uploadBytes
    The number of bytes uploaded.
    long
    totalBytes
    Total number of bytes.
    long
    result callback:TXUGCPublishTypeDef.ITXMediaPublishListener.onPublishComplete
    Parameter Name
    Parameter Description
    Type
    result
    upload result
    TXUGCPublishTypeDef.TXPublishResult
    upload result:TXUGCPublishTypeDef.TXMediaPublishResult
    Parameter Name
    Parameter Description
    Type
    retCode
    result code
    int
    descMsg
    Error description for upload failure.
    String
    mediaId
    vod media file ID.
    String
    mediaURL
    Media resource storage address.
    String
    Pre-upload:TXUGCPublishOptCenter.prepareUpload
    Parameter Name
    Parameter Description
    Type
    Required
    signature
    String
    YES

    Error code

    SDK uses the TXUGCPublishTypeDef.ITXVideoPublishListener\\ITXMediaPublishListener interface to subscribe to the status of video uploading. Therefore, you can use the retCode in TXUGCPublishTypeDef.TXPublishResult\\TXMediaPublishResult to determine the situation of video uploading.
    Status code
    Corresponding constant in TVCConstants
    Meaning
    0
    NO_ERROR
    Upload successful.
    1001
    ERR_UGC_REQUEST_FAILED
    Request upload failed, usually due to an expired or invalid client signature, requiring the app to reapply for a signature.
    1002
    ERR_UGC_PARSE_FAILED
    Failed to parse request information.
    1003
    ERR_UPLOAD_VIDEO_FAILED
    Failed to upload video.
    1004
    ERR_UPLOAD_COVER_FAILED
    Failed to upload cover.
    1005
    ERR_UGC_FINISH_REQUEST_FAILED
    Failed to end upload request.
    1006
    ERR_UGC_FINISH_RESPONSE_FAILED
    End upload response error.
    1007
    ERR_CLIENT_BUSY
    Client is busy (object cannot handle more requests).
    1008
    ERR_FILE_NOEXIT
    Uploaded file does not exist.
    1009
    ERR_UGC_PUBLISHING
    Video is currently being uploaded.
    1010
    ERR_UGC_INVALID_PARAM
    Video is currently being uploaded.
    1012
    ERR_UGC_INVALID_SIGNATURE
    Video upload signature is empty.
    1013
    ERR_UGC_INVALID_VIDOPATH
    Path to video file is empty.
    1014
    ERR_UGC_INVALID_VIDEO_FILE
    Video file does not exist at current path.
    1015
    ERR_UGC_FILE_NAME
    Video upload file name is too long (exceeds 40 characters) or contains special characters.
    1016
    ERR_UGC_INVALID_COVER_PATH
    Incorrect cover path for video file, file does not exist.
    1017
    ERR_USER_CANCEL
    User canceled upload.
    1020
    ERR_UPLOAD_SIGN_EXPIRED
    Signature expired.
    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support