Skip to content

Most visited

Recently visited

navigation

传输资源

本课内容

  1. 传输资源
  2. 接收资源

相关示例

要通过蓝牙传输发送图像等庞大的二进制数据 blob,您可以将 Asset 连接到数据项,然后将数据项放入重复数据存储。

:尽管 Wear 应用可以使用下文引用的 Data Layer API 与手机应用通信,但不建议使用此 API 连接网络

资源自动处理数据的缓存,以防止重新传输和节省蓝牙带宽。一种常见模式是,手持式设备应用下载图像,将其缩小到适合在穿戴式设备上显示的尺寸,然后将其作为资源传输到穿戴式设备应用。以下示例演示了这一模式。

:尽管数据项的大小限制在 100KB,但资源可以是所需的任意大小。不过,在许多情况下传送大型资源都会影响用户体验,因此请测试您的应用,以确保应用在传送大型资源时保持良好性能。

传输资源

使用 Asset 类中的其中一个 create...() 函数创建资源。本例中,我们将一个位图转换成字节流,然后调用 createFromBytes() 来创建资源。

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

得到一个资源后,在 DataMapPutDataRequest 中使用 putAsset() 函数将它与一个数据项相连,然后使用 putDataItem() 将数据项放入数据存储:

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

接收资源

资源创建后,您可能想在连接的另一端读取并提取它。下面是一个有关如何实现回调来检测资源变化和提取资源的示例:

@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);
}
This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Follow Google Developers on WeChat

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience.
(Sep 2017 survey)