1page.title=Transferring Assets 2 3@jd:body 4 5<div id="tb-wrapper"> 6<div id="tb"> 7 8<h2>This lesson teaches you to</h2> 9<ol> 10 <li><a href="#TransferAsset">Transfer an Asset</a></li> 11 <li><a href="#ReceiveAsset">Receive an Asset</a></li> 12</ol> 13 14</div> 15</div> 16 17<p> 18To send large blobs of binary data over the Bluetooth transport, such as images, attach an 19<a href="{@docRoot}reference/com/google/android/gms/wearable/Asset.html"><code>Asset</code></a> to a 20data item and the put the data item into the replicated data store. 21</p> 22 23<p>Assets automatically handle caching of data to prevent retransmission and conserve Bluetooth bandwidth. 24A common pattern is for a handheld app to download an image, shrink it to an appropriate size 25for display on the wearable, and transmit it to the wearable app as an asset. The following examples 26demonstrate this pattern. 27</p> 28 29<p class="note"><b>Note:</b> Although the size of data items is limited to 100KB, 30assets can be as large as desired. However, transferring large assets affects the 31user experience in many cases, so test your apps to ensure that they perform well 32if you're transferring large assets. 33<p> 34 35<h2 id="TransferAsset">Transfer an Asset</h2> 36<p>Create the asset using one of the <code>create...()</code> methods in the 37<a href="{@docRoot}reference/com/google/android/gms/wearable/Asset.html"><code>Asset</code></a> class. 38Here, we convert a bitmap to a byte stream and then call 39<a href="{@docRoot}reference/com/google/android/gms/wearable/Asset.html#createFromBytes(byte[])"><code>createFromBytes()</code></a> 40to create the asset. 41</p> 42 43<pre> 44private static Asset createAssetFromBitmap(Bitmap bitmap) { 45 final ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); 46 bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteStream); 47 return Asset.createFromBytes(byteStream.toByteArray()); 48} 49</pre> 50 51<p>When you have an asset, attach it to a data item with the <code>putAsset()</code> method in 52<a href="{@docRoot}reference/com/google/android/gms/wearable/DataMap.html"><code>DataMap</code></a> 53or 54<a href="{@docRoot}reference/com/google/android/gms/wearable/PutDataRequest.html"><code>PutDataRequest</code></a> 55and then put the data item into the data store with 56<a href="{@docRoot}reference/com/google/android/gms/wearable/DataApi.html#putDataItem(com.google.android.gms.common.api.GoogleApiClient, com.google.android.gms.wearable.PutDataRequest)"><code>putDataItem()</code></a>: 57</p> 58 59<p><b>Using PutDataRequest</b></p> 60<pre> 61Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image); 62Asset asset = createAssetFromBitmap(bitmap); 63PutDataRequest request = PutDataRequest.create("/image"); 64request.putAsset("profileImage", asset); 65Wearable.DataApi.putDataItem(mGoogleApiClient, request); 66</pre> 67 68<p><b>Using PutDataMapRequest</b></p> 69<pre> 70Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image); 71Asset asset = createAssetFromBitmap(bitmap); 72PutDataMapRequest dataMap = PutDataMapRequest.create("/image"); 73dataMap.getDataMap().putAsset("profileImage", asset) 74PutDataRequest request = dataMap.asPutDataRequest(); 75PendingResult<DataApi.DataItemResult> pendingResult = Wearable.DataApi 76 .putDataItem(mGoogleApiClient, request); 77</pre> 78 79 80<h2 id="ReceiveAsset">Receive assets</h2> 81 82<p> 83When an asset is created, you probably want to read and extract 84it on other side of the connection. Here's an example of how to implement the 85callback to detect an asset change and extract the asset: 86</p> 87 88<pre> 89@Override 90public void onDataChanged(DataEventBuffer dataEvents) { 91 for (DataEvent event : dataEvents) { 92 if (event.getType() == DataEvent.TYPE_CHANGED && 93 event.getDataItem().getUri().getPath().equals("/image")) { 94 DataMapItem dataMapItem = DataMapItem.fromDataItem(event.getDataItem()); 95 Asset profileAsset = dataMapItem.getDataMap().getAsset("profileImage"); 96 Bitmap bitmap = loadBitmapFromAsset(profileAsset); 97 // Do something with the bitmap 98 } 99 } 100} 101 102public Bitmap loadBitmapFromAsset(Asset asset) { 103 if (asset == null) { 104 throw new IllegalArgumentException("Asset must be non-null"); 105 } 106 ConnectionResult result = 107 mGoogleApiClient.blockingConnect(TIMEOUT_MS, TimeUnit.MILLISECONDS); 108 if (!result.isSuccess()) { 109 return null; 110 } 111 // convert asset into a file descriptor and block until it's ready 112 InputStream assetInputStream = Wearable.DataApi.getFdForAsset( 113 mGoogleApiClient, asset).await().getInputStream(); 114 mGoogleApiClient.disconnect(); 115 116 if (assetInputStream == null) { 117 Log.w(TAG, "Requested an unknown Asset."); 118 return null; 119 } 120 // decode the stream into a bitmap 121 return BitmapFactory.decodeStream(assetInputStream); 122} 123</pre> 124