Home | History | Annotate | Download | only in data-layer
      1 page.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>
     18 To 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
     20 data 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.
     24 A common pattern is for a handheld app to download an image, shrink it to an appropriate size
     25 for display on the wearable, and transmit it to the wearable app as an asset. The following examples
     26 demonstrate this pattern.
     27 </p>
     28 
     29 <p class="note"><b>Note:</b> Although the size of data items is limited to 100KB,
     30 assets can be as large as desired. However, transferring large assets affects the
     31 user experience in many cases, so test your apps to ensure that they perform well
     32 if 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.
     38 Here, 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>
     40 to create the asset.
     41 </p>
     42 
     43 <pre>
     44 private 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>
     53 or
     54 <a href="{@docRoot}reference/com/google/android/gms/wearable/PutDataRequest.html"><code>PutDataRequest</code></a>
     55 and 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>
     61 Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
     62 Asset asset = createAssetFromBitmap(bitmap);
     63 PutDataRequest request = PutDataRequest.create("/image");
     64 request.putAsset("profileImage", asset);
     65 Wearable.DataApi.putDataItem(mGoogleApiClient, request);
     66 </pre>
     67 
     68 <p><b>Using PutDataMapRequest</b></p>
     69 <pre>
     70 Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
     71 Asset asset = createAssetFromBitmap(bitmap);
     72 PutDataMapRequest dataMap = PutDataMapRequest.create("/image");
     73 dataMap.getDataMap().putAsset("profileImage", asset)
     74 PutDataRequest request = dataMap.asPutDataRequest();
     75 PendingResult&lt;DataApi.DataItemResult&gt; pendingResult = Wearable.DataApi
     76         .putDataItem(mGoogleApiClient, request);
     77 </pre>
     78 
     79 
     80 <h2 id="ReceiveAsset">Receive assets</h2>
     81 
     82 <p>
     83 When an asset is created, you probably want to read and extract
     84 it on other side of the connection. Here's an example of how to implement the
     85 callback to detect an asset change and extract the asset:
     86 </p>
     87 
     88 <pre>
     89 &#64;Override
     90 public 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 
    102 public 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