私は同様の問題を抱えていることに気づきました。いくつかの調査とテストの後、私はその主題に役立つ多くの方法を思いつきました. これらは Mono for Android を使用して C# で実装されていますが、Java とほぼ同じであると思います。
/// <summary>
///Calculates the memory bytes used by the given Bitmap.
/// </summary>
public static long GetBitmapSize(Android.Graphics.Bitmap bmp)
{
return GetBitmapSize(bmp.Width, bmp.Height, bmp.GetConfig());
}
/// <summary>
///Calculates the memory bytes used by a Bitmap with the given specification.
/// </summary>
public static long GetBitmapSize(long bmpwidth, long bmpheight, Android.Graphics.Bitmap.Config config)
{
int BytesxPixel = (config == Android.Graphics.Bitmap.Config.Rgb565) ? 2 : 4;
return bmpwidth * bmpheight * BytesxPixel;
}
/// <summary>
///Calculates the memory available in Android's VM.
/// </summary>
public static long FreeMemory()
{
return Java.Lang.Runtime.GetRuntime().MaxMemory() - Android.OS.Debug.NativeHeapAllocatedSize;
}
/// <summary>
///Checks if Android's VM has enough memory for a Bitmap with the given specification.
/// </summary>
public static bool CheckBitmapFitsInMemory(long bmpwidth, long bmpheight, Android.Graphics.Bitmap.Config config)
{
return (GetBitmapSize(bmpwidth, bmpheight, config) < FreeMemory());
}
このコードは、メモリ不足の例外を防ぐのに非常に信頼できることが証明されました。Utilsという名前空間でこれらのメソッドを使用する例は、以下のコード スニペットです。このコードは、3 つのビットマップに必要なメモリを計算します。そのうちの 2 つは最初のビットマップの 3 倍の大きさです。
/// <summary>
/// Checks if there's enough memory in the VM for managing required bitmaps.
/// </summary>
private bool NotEnoughMemory()
{
long bytes1 = Utils.GetBitmapSize(this.Width, this.Height, BitmapConfig);
long bytes2 = Utils.GetBitmapSize(this.Width * 3, this.Height * 3, BitmapConfig);
return ((bytes1 + bytes2 + bytes2) >= Utils.FreeMemory());
}