0

smartwatchから、文字列(タイトル、サブタイトル、コンテンツ...)のリストを含むDataMapオブジェクトと、資産のリストを含むもう1つのDataMapオブジェクトを含むDataMapオブジェクトを受け取りますAndroidはBitmapへの複数のInputStreamデコードを受けます

List<DataMap> rootItemDataMap [...] 
    DataMap itemFieldsDataMap = rootItem.getDataMap(Constants.ROOT_ITEM_FIELDS); 
    DataMap itemImagesDataMap = rootItem.getDataMap(Constants.ROOT_ITEM_IMAGES); 

私はonDataChangedが呼び出されているときに、StringのitemFieldDataMapとAssetsのリストのitemImagesDataMapを変換します。各資産について、資産をビットマップに変換し、それをビットマップのリストに入れたいと思っています。私は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); 
} 

悪い仕事をする方法があるloadBitmapFromAsset:私はすでにBitmapFactoryを使用してみましたが

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) 

:私は、実行時にこの例外を取得

public static Bitmap loadBitmapFromAsset(GoogleApiClient mGoogleApiClient, Asset asset) { 
    InputStream assetInputStream = Wearable.DataApi.getFdForAsset(mGoogleApiClient, asset).await().getInputStream(); 
    return BitmapFactory.decodeStream(assetInputStream); 
} 

。オプション、しかし私はこれを読んだSkImageDecoder::Factory returned null

WearList内の画像を表示するには、itemImagesDataMapに1つのAssetしか含まれていないため、受け取った各アイテムの最初のイメージを取得します。 forループで複数のビットマップを読み込むにはどうしたらいいですか?Android Wearで、一般的にAndroidでメモリエラーが発生することはありませんか?

+0

時計に送信する前に画像を適切なサイズにスケーリングしていますか? – String

+0

いいえ、時計の解像度に基づいて画像の適切なサイズを動的に決定する方法がわかりません。 – Panda

+0

ちょうど推測(たとえば400ピクセル)するか、データAPIを使用してサイズをウォッチから電話に送信します。 – String

答えて

0

それがいることを言及した前記あなたが最初のチェックLoading Large Bitmaps Efficientlyにしたいことがあります。あなたが示すように、ターゲットの幅と高さに基づいて、2つの電源であるサンプルサイズの値を計算する方法を使用してみて

To avoid java.lang.OutOfMemory exceptions, check the dimensions of a bitmap before decoding it, unless you absolutely trust the source to provide you with predictably sized image data that comfortably fits within the available memory.

documentationのサンプルコードを参照してください。

  • としてinSampleSizeを使用してメモリ消費量を減らすことによってOutOfMemoryエラーを修正:として、さらにさらにあなたを助けるために、

    To use this method, first decode with inJustDecodeBounds set to true , pass the options through and then decode again using the new inSampleSize value and inJustDecodeBounds set to false .

    を議論ポストはそう、あなたは以下の関連で提案されているソリューションをチェックすることもできますこのSO post

  • で提案されているように、外部ストレージからビットマップを取得するには、hereという静的メソッドを使用します。
+0

BitmapFactory.OptionとinJustDecodeBoundsをtrueに設定してからfalseに設定してみました。これを行い、すべてのドキュメンテーションのヒントに従うことで、私はOutOfMemoryErrorを回避できますが、このメッセージが表示されます - > "SkImageDecoder :: Factory returned null" イメージが正しく装着されていません。このメッセージについてのいくつかの記事はオンラインであり、状況をどのように処理するのか理解できません – Panda

関連する問題