From the smartwatch I receive a DataMap object which contains two DataMap Objects, one containing a list of strings (title, subtitle, content ...) and the other one containing a list of Assets
List<DataMap> rootItemDataMap [...]
DataMap itemFieldsDataMap = rootItem.getDataMap(Constants.ROOT_ITEM_FIELDS);
DataMap itemImagesDataMap = rootItem.getDataMap(Constants.ROOT_ITEM_IMAGES);
when onDataChanged is being called, I convert itemFieldDataMap in Strings and itemImagesDataMap in a list of Assets. for each Asset I want to convert the Asset in Bitmap and put it in a list of Bitmaps. but I have an OutOfMemoryError:
List<Bitmaps> imagesList = new ArrayList<>();
for (int j = 0; j < itemImagesDataMap.size(); j++) {
Asset asset = itemImagesDataMap.getAsset(Constants.EXTRA_IMAGE +j);
Bitmap bitmap = WearableUtils.loadBitmapFromAsset(mGoogleApiClient, asset);
imagesList.add(bitmap);
}
the method that do the bad job is loadBitmapFromAsset:
public static Bitmap loadBitmapFromAsset(GoogleApiClient mGoogleApiClient, Asset asset) {
InputStream assetInputStream = Wearable.DataApi.getFdForAsset(mGoogleApiClient, asset).await().getInputStream();
return BitmapFactory.decodeStream(assetInputStream);
}
I get this exception at runtime:
java.lang.OutOfMemoryError: Failed to allocate a 2531852 byte allocation with 1037608 free bytes and 1013KB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
at android.graphics.BitmapFactory.decodeStreamInternal(BitmapFactory.java:635)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:611)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:649)
at com.mangomobi.showtime.contentmanager.WearableUtils.loadBitmapFromAsset(WearableUtils.java:71)
at com.mangomobi.showtime.contentmanager.ItemFactoryImpl.createItems(ItemFactoryImpl.java:48)
at com.mangomobi.showtime.contentmanager.WearableContentServiceImpl.onDataChanged(WearableContentServiceImpl.java:136)
at com.google.android.gms.wearable.WearableListenerService$zzc$1.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at com.google.android.gms.wearable.WearableListenerService$zzb.dispatchMessage(Unknown Source)
at android.os.Looper.loop(Looper.java:148)
at android.os.HandlerThread.run(HandlerThread.java:61)
I have already tried using BitmapFactory.Options, but I read this SkImageDecoder::Factory returned null
The only way to properly show images in my WearList is to get the first image for each item received, as that itemImagesDataMap contains only one Asset. How can I load multiple Bitmaps in a for loop without getting memory errors in Android Wear, and generally speaking, in Android?