All Products
Search
Document Center

Object Storage Service:Display the progress bar by using OSS SDK for Android

Last Updated:Feb 20, 2024

You can use a progress bar to indicate the progress of an object that is being uploaded or downloaded. The GetObject operation is used in the topic to describe how to display the progress bar of an object that is being downloaded.

Examples

The following sample code provides an example on how to display the progress of an object named exampleobject.txt that is being downloaded from a bucket named examplebucket:

// Construct a download request. 
// Specify the name of the bucket and the full path of the object. In this example, the bucket name is examplebucket and the full path of the object is exampledir/exampleobject.txt. Do not include the bucket name in the full path. 
GetObjectRequest get = new GetObjectRequest("examplebucket", "exampledir/exampleobject.txt");
// Configure the progress callback function to display the progress bar. 
get.setProgressListener(new OSSProgressCallback<GetObjectRequest>() {
    @Override
    public void onProgress(GetObjectRequest request, long currentSize, long totalSize) {
        Log.d("GetObject", "currentSize: " + currentSize + " totalSize: " + totalSize);
    }
});
// Asynchronously download the object. 
OSSAsyncTask task = oss.asyncGetObject(get, new OSSCompletedCallback<GetObjectRequest, GetObjectResult>() {
    @Override
    public void onSuccess(GetObjectRequest request, GetObjectResult result) {
        Log.d("GetObject", "downLoadSuccess");
    }

    @Override
    public void onFailure(GetObjectRequest request, ClientException clientException, ServiceException serviceException) {
        // Handle request exceptions. 
        if (clientException != null) {
            // Handle client-side exceptions, such as network errors. 
            clientException.printStackTrace();
        }
        if (serviceException != null) {
            // Handle server-side exceptions. 
            Log.e("ErrorCode", serviceException.getErrorCode());
            Log.e("RequestId", serviceException.getRequestId());
            Log.e("HostId", serviceException.getHostId());
            Log.e("RawMessage", serviceException.getRawMessage());
        }
    }
});

References

  • For the complete sample code on how to use the progress bar during object download, visit GitHub.

  • For more information about how to initialize an OSSClient instance, see Initialization.