アップロードの再開
ファイルアップロードは、受動的な理由(例:ネットワーク断)または能動的な理由(例:ユーザー操作)によって中断されることがあります。中断されたアップロードは、後ほど中断したポイントから再開可能です。
中断したファイルアップロードを再開する例を以下に挙げます。
-
// Instantiate a bucket. KiiBucket bucket = Kii.bucket("AppBucket"); // Get the transfer manager of the bucket. KiiRTransferManager manager = bucket.getTransferManager(); // Prepare an array for storing KiiUploader instances. List<KiiUploader> uploaders = null; try { // Get all KiiUploader instances. uploaders = manager.listUploadEntries(getApplicationContext()); } catch (StateStoreAccessException e1) { // Failed to access the local storage. } // Choose the uploader to resume by a method such as the user's choice. // This sample code simply chooses the first one. KiiUploader uploader = uploaders.get(0); try { // Resume uploading. uploader.transfer(new KiiRTransferProgressCallback() { @Override public void onProgress(KiiRTransfer operator, long completedInBytes, long totalSizeinBytes) { float progress = (float)completedInBytes / (float)totalSizeinBytes * 100.0f; } }); } catch (AlreadyStartedException e) { // The upload is already in progress. } catch (SuspendedException e) { // The upload has been suspended because of a network error or user interruption. } catch (TerminatedException e) { // The upload has been terminated because of the missing file or user interruption. } catch (StateStoreAccessException e) { // Failed to access the local storage. }
-
// Instantiate a bucket. KiiBucket bucket = Kii.bucket("AppBucket"); // Get the transfer manager of the bucket. KiiRTransferManager manager = bucket.getTransferManager(); // Get all KiiUploader instances. manager.listUploadEntries(getApplicationContext(), new KiiRTransferManagerCallback() { @Override public void listUploadEntriesCompleted(KiiRTransferManager manager, List<KiiUploader> uploaders, Exception e) { if (e != null) { // Handle the error. return; } // Choose the uploader to resume by a method such as the user's choice. // This sample code simply chooses the first one. KiiUploader uploader = uploaders.get(0); // Resume uploading. uploader.transferAsync(new KiiRTransferCallback() { @Override public void onStart(KiiRTransfer operator) { } @Override public void onProgress(KiiRTransfer operator, long completedInBytes, long totalSizeinBytes) { float progress = (float) completedInBytes / (float) totalSizeinBytes * 100.0f; } @Override public void onTransferCompleted(KiiRTransfer operator, Exception exception) { if (exception != null) { // Handle the error. return; } } }); } });
ここでは以下の処理を行っています。
- アップロード再開対象ファイルが紐付いている KiiObject が存在する Bucket のインスタンスを作成。
getTransferManager()
メソッドを実行して、KiiRTransferManager
インスタンスを作成。listUploadEntries()
メソッドを実行して、KiiUploader
インスタンス一覧を取得。- 再開する
KiiUploader
インスタンスのtransfer()
メソッドを実行して、アップロードを再開。
KiiUploader の状態確認
上記のサンプルコードでは、KiiRTransferManager
から KiiUploader
のリストを取得しています。この際、それぞれの KiiUploader
インスタンスに対して転送状態を確認することができます。
KiiUplaoder
から転送済みのバイト数、転送予定の全バイト数、状態(転送中/停止中など)を取得する方法を以下に挙げます。
-
// Get the uploader by the method shown in the above sample code. KiiUploader uploader = uploaders.get(0); // Prepare a variable for storing the progress information. KiiRTransferInfo info = null; try { // Get the progress information of the uploader. info = uploader.info(); } catch (StateStoreAccessException e) { // Handle the error. return; } long completedSize = info.getCompletedSizeInBytes(); long totalSize = info.getTotalSizeInBytes(); KiiRTransferStatus status = info.getStatus();
-
// Get the uploader by the method shown in the above sample code. KiiUploader uploader = uploaders.get(0); // Get the progress information of the uploader. uploader.infoAsync(new KiiRTransferCallback() { @Override public void onInfoCompleted(KiiRTransfer operator, KiiRTransferInfo info, Exception e) { if (e != null) { // Handle the error. return; } long completedSize = info.getCompletedSizeInBytes(); long totalSize = info.getTotalSizeInBytes(); KiiRTransferStatus status = info.getStatus(); } });