2016-12-21 5 views
1

アクティビティのレイアウトを開始するたびに、そのアクティビティのレイアウトの背景イメージを変更したいと考えています。そのため、onResume()の内側に、私はランダムに、このようなレイアウトの背景画像を設定しています:java.lang.OutOfMemoryError:16777216の空きバイトを持つ756946956バイト割り当てとOOMまで433MBの割り当てに失敗しました

mBackgroundLayout.setBackground(ResourcesCompat.getDrawable(getResources(), homeBackgroundImage[randNumber], null)); 

これはlogcat出力です:

@Override 
public void onResume(){ 
    super.onResume(); 
    Random r = new Random(); 
    int randNumber = r.nextInt(homeBackgroundImage.length); 
    mBackgroundLayout.setBackground(ResourcesCompat.getDrawable(getResources(), homeBackgroundImage[randNumber], null)); 
} 

をしかし、私は次の行で例外を取得しています:

java.lang.OutOfMemoryError: Failed to allocate a 756946956 byte allocation with 16777216 free bytes and 433MB until OOM 
                     at dalvik.system.VMRuntime.newNonMovableArray(Native Method) 
                     at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) 
                     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609) 
                     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444) 
                     at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1080) 
                     at android.content.res.Resources.loadDrawableForCookie(Resources.java:2635) 
                     at android.content.res.Resources.loadDrawable(Resources.java:2540) 
                     at android.content.res.Resources.getDrawable(Resources.java:806) 
                     at android.support.v4.content.res.ResourcesCompatApi21.getDrawable(ResourcesCompatApi21.java:27) 
                     at android.support.v4.content.res.ResourcesCompat.getDrawable(ResourcesCompat.java:60) 
                     at in.avara.app.avaravrplayer.Activity.MainActivity.onResume(MainActivity.java:167) 
                     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1258) 
                     at android.app.Activity.performResume(Activity.java:6327) 
                     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3092) 
                     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3134) 
                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2481) 
                     at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                     at android.os.Looper.loop(Looper.java:148) 
                     at android.app.ActivityThread.main(ActivityThread.java:5417) 
                     at java.lang.reflect.Method.invoke(Native Method) 
                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

誰か、私に教えてくださいレイアウトの背景イメージをランダムに変更する最適化された方法は何ですか?

注:私はすでにandroid:hardwareAccelerated="false"android:largeHeap="true"を追加しようとしています。しかし、私の場合はうまくいかなかった。

ありがとうございます!

+0

画像のサイズは?各デバイスの解像度ごとにイメージを作成しましたか? – fbwnd

+0

メモリ内に756MBを割り当てようとしています:) – hasan83

答えて

0

756946956バイト〜721MBを割り当てようとしています。これはあまりにも大きいです。

さらに、756946956バイトは13756 x 13756ピクセルイメージに相当します。これはどのAndroidの画面よりもはるかに大きいです。

故障しているイメージを特定し、解像度を大幅に下げる必要があります。

res/drawable/にこの画像を入れると、res/drawable/res/drawable-mdpi/という同義語です。あなたの画像は、画面密度が高いほどそのドロアブルを使用するとアップサンプリングされます(例えば、xhdpi)。おそらく、そのドロワブルをres/drawable/の外に移動し、密度ベースのリサンプリングを無効にするには、目的の密度の密度別のディレクトリ(たとえば、res/drawable-xhdpi/)またはres/drawable-nodpi/に移動する必要があります。

1

イメージをres/drawable /からres/drawable/drawable-nodpiに移動してみてください。ここに問題が残っている場合は、アプリケーションのマニフェストでandroid:largeHeap = "true"を入力します。

+0

drawable-nodpiフォルダへの変更が私に役立ちました –

関連する問題