エラーなしで正常に動作する背景としてビットマップを設定しています。ただし、戻るボタンを押してバックグラウンドでアクティビティに再度移動すると、上記のメモリ不足エラーが発生します。アクティビティがフォーカスを失ったときにクリアする必要があるものはありますか?
ビットマップを設定するコード:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 1;
options.inJustDecodeBounds = false;
map = BitmapFactory.decodeFile(mapFile.getAbsolutePath(), options);
次に createScaledBitmap() を使用して画像を設定します。
このメモリ不足の問題は、アクティビティを終了して戻ったときにのみ発生します。初めて正常に動作するので、メモリ内に複数のビットマップ画像を作成していると推測していますが、どこにあるのかわかりませんか?
そこに何かがある場合の decodeFile() メソッドは次のとおりです。
try {
// decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f), null, o);
// Find the correct scale value. It should be the power of 2.
final int REQUIRED_SIZE = 70;
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < REQUIRED_SIZE
|| height_tmp / 2 < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale++;
}
// decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {
}
return null;
}
どうもありがとう。