I'm trying to transfer an asset by following the android developer training which says to use this code:
@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);
}
So I have done the same thing in roughly the same way:
// Build a new GoogleApiClient for the Wearable API
googleClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(Bundle bundle) {
Wearable.DataApi.addListener(googleClient, onDataChangedListener);
}
@Override
public void onConnectionSuspended(int i) {
}
})
.addApi(Wearable.API)
.build();
googleClient.connect();
and in my onDatachanged method I have:
public DataApi.DataListener onDataChangedListener = new DataApi.DataListener() {
@Override
public void onDataChanged(DataEventBuffer dataEvents) {
Log.d(TAG, "Data changed: " + dataEvents);
for (DataEvent event : dataEvents) {
Log.d(TAG, "Data received: " + event.getDataItem().getUri());
if (event.getType() == DataEvent.TYPE_CHANGED &&
event.getDataItem().getUri().getPath().equals("/audio")) {
DataMapItem dataMapItem = DataMapItem.fromDataItem(event.getDataItem());
Asset audioAsset = dataMapItem.getDataMap().getAsset("audioAsset");
audioBytes = loadBytesFromAsset(audioAsset);
}
// Set play button enabled
handler.post(onNewAudio());
}
}
}
with my loadBytesFromAsset() method:
public byte[] loadBytesFromAsset(Asset asset) {
if (asset == null) {
throw new IllegalArgumentException("Asset must be non-null");
}
result = googleClient.blockingConnect(3000, TimeUnit.MILLISECONDS);
if(!result.isSuccess()){
return null;
}
// Convert asset into a file descriptor and block until it's ready
InputStream assetInputStream = Wearable.DataApi.getFdForAsset(googleClient, asset).await().getInputStream();
googleClient.disconnect();
if (assetInputStream == null) {
Log.w(TAG, "Requested an unknown Asset.");
return null;
}
// Decode the stream into a byte[]
return getBytesFromInputStream(assetInputStream);
}
This seems to be doing exactly as the Android developer training suggests, but when I run it, the 'loadBytesFromAsset()' method crashes with an exception saying I can't call blockingConnect() on the UI thread. Does anyone know how to solve this? How should I be listening for and then retrieving assets? Thanks in advance.