ダウンロードの再開
ファイルダウンロードは、受動的な理由(例:ネットワーク断)または能動的な理由(例:ユーザー操作)によって中断されることがあります。中断されたダウンロードは、後ほど中断したポイントから再開できます。
中断したファイルダウンロードを再開する例を以下に挙げます。
-
// Instantiate a bucket. KiiBucket bucket = Kii.bucket("mydata"); // Get the transfer manager of the bucket. KiiRTransferManager manager = bucket.getTransferManager(); // Prepare an array for storing KiiDownloader instances. List<KiiDownloader> downloaders = null; try { // Get all KiiDownloader instances. downloaders = manager.listDownloadEntries(getApplicationContext()); } catch (StateStoreAccessException e1) { // Failed to access the local storage. } // Choose the downloader to resume by a method such as the user's choice. // This sample code simply chooses the first one. KiiDownloader downloader = downloaders.get(0); // Resume downloading. try { downloader.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 download is already in progress. } catch (SuspendedException e) { // The download has been suspended because of a network error or user interruption. } catch (TerminatedException e) { // The download 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("mydata"); // Get the transfer manager of the bucket. KiiRTransferManager manager = bucket.getTransferManager(); // Prepare an array for storing KiiDownloader instances. List<KiiDownloader> downloaders = null; try { // Get all KiiDownloader instances. downloaders = manager.listDownloadEntries(getApplicationContext()); } catch (StateStoreAccessException e1) { // Failed to access the local storage. } // Choose the downloader to resume by a method such as the user's choice. // This sample code simply chooses the first one. KiiDownloader downloader = downloaders.get(0); // Resume downloading. downloader.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
インスタンスを作成。listDownloadEntries()
メソッドを実行して、KiiDownloader
インスタンス一覧を取得。- 再開する
KiiDownloader
インスタンスのtransfer()
メソッドを実行して、ダウンロードを再開。
KiiDownloader の状態確認
上記のサンプルコードでは、KiiRTransferManager
から KiiDownloader
のリストを取得しています。この際、それぞれの KiiDownloader
インスタンスに対して転送状態を確認することができます。
KiiDownloader
から転送済みのバイト数、転送予定の全バイト数、状態(転送中/停止中など)を取得できます。取得方法は、KiiUploader の状態確認 と同様です。