Tous les produits
Search
Centre de documentation

Object Storage Service:Progress bar (Android SDK)

Dernière mise à jour :Aug 18, 2026

Utilisez une barre de progression pour suivre l'avancement du chargement ou du téléchargement d'un objet.

Notes d'utilisation

  • Avant d'exécuter l'exemple de code de cette rubrique, créez une instance OSSClient, par exemple en utilisant un nom de domaine personnalisé ou le Security Token Service (STS). Pour plus d'informations, consultez la section Initialisation (SDK Android).

Exemples

L'exemple de code suivant illustre l'utilisation d'une barre de progression lors du chargement asynchrone d'un objet avec la méthode asyncPutObject :

// Create an upload request.
// Specify the bucket name (for example, examplebucket), the full path of the object (for example, exampledir/exampleobject.txt), and the full path of the local file (for example, /storage/emulated/0/oss/examplefile.txt).
// Do not include the bucket name in the full path of the object.
PutObjectRequest put = new PutObjectRequest("examplebucket", "exampledir/exampleobject.txt", "/storage/emulated/0/oss/examplefile.txt");

// Set the progress callback function to display the progress bar.
put.setProgressCallback(new OSSProgressCallback<PutObjectRequest>() {
    @Override
    public void onProgress(PutObjectRequest request, long currentSize, long totalSize) {
        // currentSize is the size of the uploaded part. Unit: bytes.
        // totalSize is the total size of the file to upload. Unit: bytes.
        Log.d("PutObject", "currentSize: " + currentSize + " totalSize: " + totalSize);
    }
});

// Upload the file asynchronously.
OSSAsyncTask task = oss.asyncPutObject(put, new OSSCompletedCallback<PutObjectRequest, PutObjectResult>() {
    @Override
    public void onSuccess(PutObjectRequest request, PutObjectResult result) {
        Log.d("PutObject", "UploadSuccess");
        Log.d("ETag", result.getETag());
        Log.d("RequestId", result.getRequestId());
    }

    @Override
    public void onFailure(PutObjectRequest request, ClientException clientExcepion, ServiceException serviceException) {
        // Request exceptions.
        if (clientExcepion != null) {
            // Client exceptions, such as network exceptions.
            clientExcepion.printStackTrace();
        }
        if (serviceException != null) {
            // Service exceptions.
            Log.e("ErrorCode", serviceException.getErrorCode());
            Log.e("RequestId", serviceException.getRequestId());
            Log.e("HostId", serviceException.getHostId());
            Log.e("RawMessage", serviceException.getRawMessage());
        }
    }
});
// task.cancel(); // Cancels the task.
// task.waitUntilFinished(); // Waits for the task to complete.

Références

Pour consulter l'exemple de code complet sur l'utilisation d'une barre de progression lors du chargement d'objets, rendez-vous sur GitHub.