0

I am storing Bitmaps in an Asset that gets stored in a DataItem using a DataItemMap that I sync with my wearables. If I iterate through all of my DataItems using:

DataItemBuffer list = api.getDataItems(connection.getClient()).await()
for(DataItem item : list) {
    ...
}

how can I get the size of each Asset?

EDIT: When I use

DataMapItem dataMapItem = DataMapItem.fromDataItem(item);
Asset asset = dataMapItem.getDataMap().getAsset(BITMAP_KEY);
int size = asset.getData().length;

I get an NPE that says:

Attempt to get length of null array

The Asset was put using:

PutDataMapRequest putRequest = PutDataMapRequest.create(path);
DataMap map = putRequest.getDataMap();

ByteArrayOutputStream stream = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.PNG, 100, stream);
stream.flush();
Asset asset = Asset.createFromBytes(stream.toByteArray());
map.putAsset(mapKey, asset);

Wearable.DataApi.putDataItem(connection.getClient(), putRequest.asPutDataRequest());
Eliezer
  • 7,209
  • 12
  • 56
  • 103

2 Answers2

3

According to this you have to read the assets through the DataApi:

GoogleApiClient client = ...;
PendingResult<DataApi.GetFdForAssetResult> pendingResult = Wearable.DataApi.getFdForAsset(client, asset);
DataApi.GetFdForAssetResult assetResult = pendingResult.await();
InputStream assetInputStream = assetResult.getInputStream();

EDIT: If you just need the size, don't rely on the asset. On the sender side, simply write the size into the DataMap:

PutDataMapRequest putRequest = PutDataMapRequest.create(path);
DataMap map = putRequest.getDataMap();

ByteArrayOutputStream stream = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.PNG, 100, stream);
stream.flush();
byte[] byteArray = stream.toByteArray();
Asset asset = Asset.createFromBytes(byteArray);
map.putAsset(mapKey, asset);
map.putLong("dataSize", byteArray.length);

Wearable.DataApi.putDataItem(connection.getClient(), putRequest.asPutDataRequest());

When you want to read the size, just do the following:

DataMapItem dataMapItem = DataMapItem.fromDataItem(item);
long size = dataMapItem.getDataMap().getLong("dataSize");
Tom
  • 5,068
  • 5
  • 29
  • 41
  • I know I can do that, but I don't want to have to read the asset to see how big it is. However, that link never mentions anything about getting the size of an asset, so I'm hoping there's an easier way to do it. – Eliezer Aug 07 '14 at 19:35
  • The DataApi doesn't know the size until the data is read. I think the best way to transfer the size is to put it into the data map on the sender side when adding the data into the data item. – Tom Aug 08 '14 at 09:38
  • I actually want to read the size on the sender side, but it could be a week after I put it, so I don't have the original data to measure. – Eliezer Aug 08 '14 at 13:39
  • Yes, that's fine. Just put the data size into the DataMap at the time you have the data. (e.g. byte[] data = ...; map.putLong("dataSize" , data.length; map.putAsset(mapKey, asset);). It should still be readable a week later on both sides. – Tom Aug 08 '14 at 14:36
  • Since no one else gave an API answer, I'm gonna assume this is the best way to do it. Thanks! – Eliezer Aug 08 '14 at 18:23
  • Don't forget to delete the assets when possible, cause it seems (in the emulator) that _/data/data/com.google.android.gms_ is the place where they are stored persistently. I even ended up on this screen on the watch http://i.imgur.com/eH592wx.png but now way to purge anything from there. – Aladin Q Aug 25 '14 at 15:12
-1

It looks like you're adding the asset to the wrong map. you're doing "putRequest.asPutDataRequest" and not "map.asPutDataRequest". Do this:

PutDataMapRequest putRequest = PutDataMapRequest.create(path);

ByteArrayOutputStream stream = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.PNG, 100, stream);
stream.flush();
Asset asset = Asset.createFromBytes(stream.toByteArray());
putRequest.putAsset(mapKey, asset);

Wearable.DataApi.putDataItem(connection.getClient(), putRequest.asPutDataRequest());

Then you shouldn't get that NPE anymore.

sebirdman
  • 19
  • 3
  • `PutDataMapRequest` does not have a `putAsset` method. http://developer.android.com/reference/com/google/android/gms/wearable/PutDataMapRequest.html – Eliezer Jul 28 '14 at 03:12