In mobile phones and Wearable devices, Wearable transmits Assets (images, etc.) through Bluetooth ),

Source: Internet
Author: User

In mobile phones and Wearable devices, Wearable transmits Assets (images, etc.) through Bluetooth ),

When developing the android wear program, you often need to transmit images through Assets Bluetooth.

1. create an asset and use the create... () method. For example, transfer a Bitmap file as follows:

[Java]View plaincopyprint?
  1. Private static Asset createAssetFromBitmap (Bitmap bitmap ){
  2. Final ByteArrayOutputStream byteStream = new ByteArrayOutputStream ();
  3. Bitmap. compress (Bitmap. CompressFormat. PNG, 100, byteStream );
  4. Return Asset. createFromBytes (byteStream. toByteArray ());
  5. }

When an asset is created, you need to point it to a data item by using the putAsset () method as follows:

Use PutDataRequest

[Java]View plaincopyprint?
  1. Bitmap bitmap = BitmapFactory. decodeResource (getResources (), R. drawable. image );
  2. Asset asset = createAssetFromBitmap (bitmap );
  3. PutDataRequest request = PutDataRequest. create ("/image ");
  4. Request. putAsset ("profileImage", asset );
  5. Wearable. DataApi. putDataItem (mGoogleApiClient, request );

Use PutDataMapRequest

[Java]View plaincopyprint?
  1. Bitmap bitmap = BitmapFactory. decodeResource (getResources (), R. drawable. image );
  2. Asset asset = createAssetFromBitmap (bitmap );
  3. PutDataRequest request = PutDataRequest. create ("/image ");
  4. Request. putAsset ("profileImage", asset );
  5. Wearable. DataApi. putDataItem (mGoogleApiClient, request );

Use PutDataMapRequest

[Java]View plaincopyprint?
  1. Bitmap bitmap = BitmapFactory. decodeResource (getResources (), R. drawable. image );
  2. Asset asset = createAssetFromBitmap (bitmap );
  3. PutDataMapRequest dataMap = PutDataMapRequest. create ("/image ");
  4. DataMap. getDataMap (). putAsset ("profileImage", asset)
  5. PutDataRequest request = dataMap. asPutDataRequest ();
  6. PendingResult <DataApi. DataItemResult> pendingResult = Wearable. DataApi
  7. . PutDataItem (mGoogleApiClient, request );

2. The watch side receives asset

[Java]View plaincopyprint?
  1. @ Override
  2. Public void onDataChanged (DataEventBuffer dataEvents ){
  3. For (DataEvent event: dataEvents ){
  4. If (event. getType () = DataEvent. TYPE_CHANGED &&
  5. Event. getDataItem (). getUri (). getPath (). equals ("/image ")){
  6. DataMapItem dataMapItem = DataMapItem. fromDataItem (event. getDataItem ());
  7. Asset profileAsset = dataMapItem. getDataMap (). getAsset ("profileImage ");
  8. Bitmap bitmap = loadBitmapFromAsset (profileAsset );
  9. // Do something with the bitmap
  10. }
  11. }
  12. }
  13. Public Bitmap loadBitmapFromAsset (Asset asset ){
  14. If (asset = null ){
  15. Throw new IllegalArgumentException ("Asset must be non-null ");
  16. }
  17. ConnectionResult result =
  18. MGoogleApiClient. blockingConnect (TIMEOUT_MS, TimeUnit. MILLISECONDS );
  19. If (! Result. isSuccess ()){
  20. Return null;
  21. }
  22. // Convert asset into a file descriptor and block until it's ready
  23. InputStream assetInputStream = Wearable. DataApi. getFdForAsset (
  24. MGoogleApiClient, asset). await (). getInputStream ();
  25. MGoogleApiClient. disconnect ();
  26. If (assetInputStream = null ){
  27. Log. w (TAG, "Requested an unknown Asset .");
  28. Return null;
  29. }
  30. // Decode the stream into a bitmap
  31. Return BitmapFactory. decodeStream (assetInputStream );
  32. }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.