original source : https://developer.android.com/training/wearables/data-layer/assets.html

Transferring Assets

image와 같이 크기가 큰 data를 전달하는 경우, you can attach an Asset to a data item and the put the data item into the replicated data store.

data item의 경우 최대 크기가 100kb 이나 assets의 경우에는 그 제한이 없다.

Transfer an Asset

Create the asset using one of the create...() methods in the Asset class.    

private static Asset createAssetFromBitmap(Bitmap bitmap) {

final ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteStream);
    return Asset.createFromBytes(byteStream.toByteArray());

Using PutDataRequest

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
Asset asset = createAssetFromBitmap(bitmap);
PutDataRequest request = PutDataRequest.create("/image");
request.putAsset("profileImage", asset);
Wearable.DataApi.putDataItem(mGoogleApiClient, request);

Using PutDataMapRequest

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
Asset asset = createAssetFromBitmap(bitmap);
PutDataMapRequest dataMap = PutDataMapRequest.create("/image");
dataMap.getDataMap().putAsset("profileImage", asset)
PutDataRequest request = dataMap.asPutDataRequest();
PendingResult<DataApi.DataItemResult> pendingResult = Wearable.DataApi
       .putDataItem(mGoogleApiClient, request);

Receive assets

public void onDataChanged(DataEventBuffer dataEvents) {
 for (DataEvent event : dataEvents) {
   if (event.getType() == DataEvent.TYPE_CHANGED &&
       event.getDataItem().getUri().getPath().equals("/image")) {
     DataMapItem dataMapItem = DataMapItem.fromDataItem(event.getDataItem());
     Asset profileAsset = dataMapItem.getDataMap().getAsset("profileImage");
     Bitmap bitmap = loadBitmapFromAsset(profileAsset);
     // Do something with the bitmap

public Bitmap loadBitmapFromAsset(Asset asset) {
   if (asset == null) {
       throw new IllegalArgumentException("Asset must be non-null");
   ConnectionResult result =
          mGoogleApiClient.blockingConnect(TIMEOUT_MS, TimeUnit.MILLISECONDS);
   if (!result.isSuccess()) {
       return null;
   // convert asset into a file descriptor and block until it's ready
   InputStream assetInputStream = Wearable.DataApi.getFdForAsset(
           mGoogleApiClient, asset).await().getInputStream();

   if (assetInputStream == null) {
       Log.w(TAG, "Requested an unknown Asset.");
       return null;
   // decode the stream into a bitmap
   return BitmapFactory.decodeStream(assetInputStream);

Comments are closed.

Post Navigation