edit-icon download-icon

Android version

Last Updated: Sep 10, 2018

Environment requirements

Android 2.3 or a later version


OSS Android SDK

Download the upload SDK

Directly introduce the JAR package

After you download the ZIP package of the VODUpload Android SDK, perform the following steps (applicable to Android Studio or Eclipse):

  • Decompress the SDK to obtain the following JAR packages in the libs directory: aliyun-oss-sdk-android-xxx.jar, okhttp-2.7.0.jar, okio-2.6.0.jar, and aliyun-vod-upload-android-sdk-xxx.jar.
  • Import the four JAR packages to the libs directory of the project.
  • Set permissions

    The following are the Android permissions required by the VODUpload Android SDK. Make sure that these permissions are already set in your AndroidManifest.xml file. Otherwise, the SDK cannot work normally.

    1. <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    2. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
    3. <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
    4. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

Create a VODUpload instance

Set the callback function.

  1. VODUploadCallback callback = new VODUploadCallback() {
  2. /**
  3. * Triggered when the file upload starts
  4. */
  5. void onUploadStarted() {;}
  6. /**
  7. * Callback after successful upload
  8. */
  9. void onUploadSucceed(UploadFileInfo info) {;}
  10. /**
  11. * Upload failed
  12. */
  13. void onUploadFailed(UploadFileInfo info, String code, String message) {;}
  14. /**
  15. * Callback upload progress
  16. * @param uploadedSize: The number of uploaded bytes
  17. * @param totalSize: The total number of required bytes
  18. */
  19. void onUploadProgress(UploadFileInfo info, long uploadedSize, long totalSize) {;}
  20. /**
  21. * This API is called back after the upload credential expires
  22. * Obtain a new upload credential in the callback and call resumeUploadWithAuth to continue upload
  23. */
  24. void onUploadTokenExpired() {;}
  25. /**
  26. * Triggered when the status switches from normal to abnormal during upload
  27. */
  28. void onUploadRetry(String code, String message) {;}
  29. /**
  30. * Triggered when the status is resumed from abnormal during upload
  31. */
  32. void onUploadRetryResume() {;}
  33. };
  34. VODUploadClient uploader = new VODUploadClientImpl(getContext());

Initialize the upload SDK

Enter the authorization information in either of the following ways:

  • AccessKey

    It is simple but not safe. It is recommended that this way be used in the test environment.

    1. uploader.init("<accessKeyId>", "<accessKeySecret>", callback);
  • Token

    It is safe but complex. It is recommended that this way be used in the production environment. A token is temporary and valid for a period. Therefore, sending a token is safe.

    1. uploader.init("<accessKeyId>", "<accessKeySecret>", "<secretToken>", "<expireTime>", callback);

List management

  • Add a file to be uploaded

    Note: The file size cannot exceed 4 GB.

    1. uploader.addFile("<uploadFilePath>",
    2. "<endpoint>", // For example, the Hangzhou region"http://oss-cn-hangzhou.aliyuncs.com"
    3. "<bucketName>", // Enter the actual bucket name
    4. "<objectKey>");

    During uploading, obtain the attributes (the title, tag, description, category, cover URL, and custom data) of a media set in the following way: addFile contains a reload function, in which the last parameter is a VodInfo object. The definitions are as follows:

    1. private String title;
    2. private String desc;
    3. private Integer cateId;
    4. private List<String> tags;
    5. private String userData;
    6. private String coverUrl;
  • Delete the uploaded file. indexcorresponds to the index of the elements in the list returned by listFiles

    1. uploader.deleteFile(index);
    • Cancel upload of a single file in the list

      1. uploader.cancelFile(index);
    • Resume upload of a single file in the list

      1. uploader.resumeFile(index);
    • Obtain the upload file list

      1. List<UploadFileInfo> list = uploader.listFiles();
    • Clear the upload file list

      1. upload.clearFiles();

Upload control

  • Start upload

    1. uploader.start();
  • Stop upload

    1. uploader.stop();
  • Pause upload

    1. uploader.pause();
  • Resume upload

    1. uploader.resume();
  • Resume upload after the token is invalid

    1. uploader.resumeWithToken("<accessKeyId>", "<accessKeySecret>", "<secretToken>", "<expireTime>");
Thank you! We've received your feedback.