ダウンロード
中断/再開が可能な Object Body のダウンロードをする例を以下に挙げます。
-
// Instantiate the target KiiObject. Uri objUri = Uri.parse("Set the URI of an existing KiiObject here"); KiiObject object = KiiObject.createByUri(objUri); // Create a downloader. KiiDownloader downloader = null; try { // Specify the file destination. downloader = object.downloader(getApplicationContext(), new File( Environment.getExternalStorageDirectory(), "sample.mp4")); } catch (InvalidHolderException e) { // The target KiiObject has been deleted or has not been saved. } try { // Start downloading. downloader.transfer(new KiiRTransferCallback() { @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 the target KiiObject. Uri objUri = Uri.parse("Set the URI of an existing KiiObject here"); KiiObject object = KiiObject.createByUri(objUri); // Create a downloader. KiiDownloader downloader = null; try { // Specify the file destination. downloader = object.downloader(getApplicationContext(), new File( Environment.getExternalStorageDirectory(), "sample.mp4")); } catch (InvalidHolderException e) { // The target KiiObject has been deleted or has not been saved. } // Start 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; } } });
ここでは以下の処理を実施しています。
createdByUri()
メソッドを実行して、KiiObject
インスタンスを作成。- ダウンロード対象ファイルのリファレンスを指定して
downloader()
メソッドを実行し、KiiDownloader
インスタンスを作成。 transfer()
メソッドを実行して、ダウンロードを開始。
onProgress()
メソッドは、転送の進捗状況に応じて呼び出されます。転送サイズが小さい場合は、1 回目の呼び出しで 100% の進捗を示すことがあります。
何らかの理由によりダウンロードが中断した場合は SuspendedException
が発生します。この場合、中断箇所よりダウンロードの再開を行うことができます。再開方法については ダウンロードの再開 を参照してください。
Object Body のダウンロードで外部記憶装置にアクセスする際、実行時パーミッションが必要になる場合があります。実装方法の詳細は、実行時パーミッションの取得 を参照してください。