2017-05-23 9 views
0

UIに少数の画像を適用しようとしましたが、setImageResourceを使用するスペースがないため、bitmapを使用しました。 問題は割り当てに失敗したことを示します。ビットマップを使用して割り当てに失敗しました -

java.lang.OutOfMemoryError: Failed to allocate a 93600012 byte allocation with 16765312 free bytes and 70MB until OOM

この問題を解決するお手伝いができれば幸いです。

int imgResource = item.getIcon(); // for example imgResource = R.drawable.test 
Drawable drawable = context.getResources().getDrawable(imgResource); //Here I get the error 
Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap(); 
holder.iv.setImageBitmap(bitmap); 

ありがとう!

+0

画像がどのくらいあるの? (幅x高さ) –

+0

これは、https://developer.android.com/topic/performance/graphics/load-bitmap.htmlに役立ちます – Pavan

+0

@SteveSmithこのコードは機能の一部であり、関数は25個の画像とすべての画像を取得します異なります。 – japi99

答えて

0

この方法をお試しください。 android:largeHeap="true"

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:largeHeap="true" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 

使用このメソッドを追加して、コンテキストとイメージファイル名を渡します

public Bitmap getAssetImage(Context context, String filename) throws IOException { 
    AssetManager assets = context.getResources().getAssets(); 
    InputStream buffer = new BufferedInputStream((assets.open("drawable/" + filename + ".png"))); 
    Bitmap bitmap = BitmapFactory.decodeStream(buffer); 
    return bitmap; 
    // return new BitmapDrawable(context.getResources(), bitmap); 
} 
+0

私はそれを試みます、バットこれはすべてのメモリがまだいっぱいですので、これは "この問題で対処する"最高の答えではありません。 – japi99

+0

更新コードを試してください –

+0

私はそれを試してみましょうしかし今、すべての画像は白です。なぜなのかご存知ですか ? – japi99

関連する問題