Android 傳輸資源

2018-08-02 17:54 更新

編寫:wly2014 - 原文: http://developer.android.com/training/wearables/data-layer/assets.html

為了通過(guò)藍(lán)牙發(fā)送大量的二進(jìn)制數(shù)據(jù),比如圖片,要將一個(gè)Asset附加到數(shù)據(jù)元上,并放入復(fù)制而來(lái)的數(shù)據(jù)庫(kù)中。

Assets 能夠自動(dòng)地處理數(shù)據(jù)緩存以避免重復(fù)發(fā)送,保護(hù)藍(lán)牙帶寬。一般的模式是:手持設(shè)備下載圖像,將圖片壓縮到適合在可穿戴設(shè)備上顯示的大小,并以Asset傳給可穿戴設(shè)備。下面的例子演示此模式。

Note: 盡管數(shù)據(jù)元的大小限制在100KB,但資源可以任意大。然而,傳輸大量資源會(huì)多方面地影響用戶體驗(yàn),因此,當(dāng)傳輸大量資源時(shí),要測(cè)試我們的應(yīng)用以保證它有良好的用戶體驗(yàn)。

傳輸資源

在Asset類中使用creat..()方法創(chuàng)建資源。下面,我們將一個(gè)bitmap轉(zhuǎn)化為字節(jié)流,然后調(diào)用creatFromBytes())方法創(chuàng)建資源。

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

創(chuàng)建資源后,使用 DataMap 或者 PutDataRepuest 類中的 putAsset() 方法將其附加到數(shù)據(jù)元上,然后用 putDataItem() 方法將數(shù)據(jù)元放入數(shù)據(jù)庫(kù)。

使用 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);

使用 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);

接收資源

創(chuàng)建資源后,我們可能需要在另一連接端讀取資源。以下是如何實(shí)現(xiàn)回調(diào)以發(fā)現(xiàn)資源變化和提取Asset對(duì)象。

@Override
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();
            mGoogleApiClient.disconnect();

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


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)