OutOfMemoryErrorの解決方法
Androidでアプリを作成しています
下のaddBalloonというメソッド(人物画像付きの吹き出しを動的に作成したかった)を複数回呼び出すアクティビティがあるのですが、このアクティビティを開き、戻るボタン(intentで他のアクティビティに遷移)で画面を戻し、再度このアクティビティを呼び出すということを繰り返していると3回ほどでOutOfMemoryErrorが出てしまいます。Bitmap関連だろうと思いrecycle()してみているのですが、あまり効果がありません。addBalloonメソッドを複数回実行すると、最後のメソッドでのBitmapしか解放されていなかったりするのでしょうか...
当たり前なのですがaddBalloon内で最後にreleaseBitmap,releaseImageViewメソッドを呼び出すと画像は表示されなくなるのですがエラーは出なくなったので解放のタイミングなのかなと思います...
また、作成した戻るボタンではなくAndroidの機能として付いている戻るボタン?(画面左下の◁のことです)でアクティビティを戻し、再度表示するとエラーは出ないのですがintentで戻ることとは根本的に異なるのでしょうか、単純に勉強不足です...
largeHeapを使うといくらかマシにはなるのですが最終的にはエラーが出てアプリが落ちてしまうので根本的な解決策があれば教えていただきたいです。
色々調べてはみたのですが自分では力不足のようなので皆さんの知恵をお借りしたいです、よろしくお願いします。
lang
1 private void addBalloon(String str) { 2 // get Layout 3 LinearLayout linearLayout = new LinearLayout(this); 4 linearLayout.setOrientation(LinearLayout.HORIZONTAL); 5 LinearLayout mikoLayout = new LinearLayout(this); 6 mikoLayout.setOrientation(LinearLayout.VERTICAL); 7 8 // get bitmap 9 Resources r = getResources(); 10 bmp = BitmapFactory.decodeResource(r, R.drawable.miko); 11 mask = BitmapFactory.decodeResource(r, R.drawable.circle); 12 bitmap = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), Bitmap.Config.RGB_565); 13 14 // clipping 15 Canvas tempCanvas = new Canvas(bitmap); 16 Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); 17 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN)); 18 tempCanvas.drawBitmap(bmp, 0, 0, null); 19 tempCanvas.drawBitmap(mask, 0, 0, paint); 20 paint.setXfermode(null); 21 22 // get View 23 iv = new ImageView(this); 24 iv.setImageBitmap(bitmap); 25 26 TextView mikoTv = new TextView(this); 27 mikoTv.setText("xxx"); 28 mikoTv.setTextSize(18); 29 mikoTv.setGravity(Gravity.CENTER); 30 31 TextView tv = new TextView(this); 32 tv.setText(str); 33 tv.setBackgroundResource(R.drawable.fukidashi); 34 tv.setPadding(70, 30, 20, 30); 35 tv.setTextSize(18); 36 37 // get and set Layout 38 LinearLayout lay = (LinearLayout) this.findViewById(R.id.textLayout); 39 40 mikoLayout.addView(iv, 200, 200); 41 mikoLayout.addView(mikoTv); 42 linearLayout.addView(mikoLayout); 43 linearLayout.addView(tv, WC, WC); 44 45 lay.addView(linearLayout); 46 } 47 48 protected void onPause() { 49 super.onPause(); 50 releaseImageView(); 51 releaseBitmap(); 52 } 53 54 private void releaseBitmap() { 55 if (bitmap != null) { 56 bitmap.recycle(); 57 bitmap = null; 58 } 59 if (bmp != null) { 60 bmp.recycle(); 61 bmp = null; 62 } 63 if (mask != null) { 64 mask.recycle(); 65 mask = null; 66 } 67 } 68 69 private void releaseImageView(){ 70 if(iv != null){ 71 iv.setImageDrawable(null); 72 } 73 } 74
09-16 18:05:40.566 23922-23922/com.example.xxx.myapplication E/art: Throwing OutOfMemoryError "Failed to allocate a 17640012 byte allocation with 13937704 free bytes and 13MB until OOM" 09-16 18:05:40.579 23922-23922/com.example.xxx.myapplication E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.xxx.myapplication, PID: 23922 java.lang.OutOfMemoryError: Failed to allocate a 17640012 byte allocation with 13937704 free bytes and 13MB 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.BitmapFactory.decodeResource(BitmapFactory.java:467) at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:497) at com.example.xxx.myapplication.BirthResultActivity.addBalloon(BirthResultActivity.java:209) at com.example.xxx.myapplication.BirthResultActivity.showResult(BirthResultActivity.java:132) at com.example.xxx.myapplication.BirthResultActivity.onCreate(BirthResultActivity.java:57) at android.app.Activity.performCreate(Activity.java:5941) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2358) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2464) at android.app.ActivityThread.access$1000(ActivityThread.java:161) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1371) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5438) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:917) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:712)
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/09/22 05:47
2017/09/22 05:56
2017/09/22 06:10
2017/09/22 06:14
2017/09/22 06:29
2017/09/22 06:36