I am storing Bitmap
s 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 DataItem
s 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());
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");