0

我将BitmapsAsset存储在一个DataItem使用DataItemMap与我的可穿戴设备同步的 a 中。如果我使用以下方法遍历我的所有DataItems:

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

我怎样才能得到每个的大小Asset

编辑:当我使用

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

我得到一个 NPE,上面写着:

尝试获取空数组的长度

使用Asset

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());
4

2 回答 2

3

据此,必须通过 DataApi 读取资产:

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

编辑: 如果您只需要大小,请不要依赖资产。在发送方,只需将大小写入 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());

当您想读取大小时,只需执行以下操作:

DataMapItem dataMapItem = DataMapItem.fromDataItem(item);
long size = dataMapItem.getDataMap().getLong("dataSize");
于 2014-08-07T16:00:06.890 回答
-1

看起来您将资产添加到错误的地图。你正在做“putRequest.asPutDataRequest”而不是“map.asPutDataRequest”。做这个:

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());

那么你不应该再得到那个NPE了。

于 2014-07-21T07:26:52.250 回答