進行状況バーを使用して、アップロードまたはダウンロードされているオブジェクトの進行状況を示すことができます。
使用上の注意
このトピックのサンプルコードを実行する前に、カスタムドメイン名やSecurity Token Service (STS) などの方法を使用してOSSClientインスタンスを作成する必要があります。 詳細については、「初期化」をご参照ください。
例
次のサンプルコードは、asyncPutObjectを使用してオブジェクトを非同期でアップロードするときにプログレスバーを使用する方法の例を示しています。
// Construct an upload request.
// Specify the name of the bucket, the full path of the object, and the full path of the local file. In this example, the name of the bucket is examplebucket, the full path of the object is exampledir/exampleobject.txt, and the full path of the local file is /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");
// Configure 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 specifies the size of the uploaded part of the object. Unit: bytes.
// totalSize specifies the total size of the object that you want to upload. Unit: bytes.
Log.d("PutObject", "currentSize: " + currentSize + " totalSize: " + totalSize);
}
});
// Aasynchronously upload the local file.
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) {
// Handle request exceptions.
if (clientExcepion != null) {
// Handle client exceptions, such as network exceptions.
clientExcepion.printStackTrace();
}
if (serviceException != null) {
// Handle service exceptions.
Log.e("ErrorCode", serviceException.getErrorCode());
Log.e("RequestId", serviceException.getRequestId());
Log.e("HostId", serviceException.getHostId());
Log.e("RawMessage", serviceException.getRawMessage());
}
}
});
// task.cancel(); // You can cancel the download task.
// task.waitUntilFinished(); // Wait until the task is complete.
参考資料
オブジェクトのアップロード時に進行状況バーを使用する方法の完全なサンプルコードについては、GitHubをご覧ください。