public class MainActivity extends Activity implements OnClickListener {
ImageView imageView1;
Bitmap s;
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
// logHeap("ECAonCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView1 = (ImageView) findViewById(R.id.imageView1);
imageView1.setBackgroundResource(R.drawable.launchimage20481496);
// s = decodeFil3e(R.drawable.launchimage20481496);
ImageButton btn = (ImageButton) findViewById(R.id.btnEnter);
btn.setOnClickListener(this);
}
public int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
// Calculate ratios of height and width to requested height and
// width
final int heightRatio = Math.round((float) height
/ (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
private Bitmap decodeFil3e(int f) {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
o.inTempStorage = new byte[16 * 1024];
BitmapFactory.decodeResource(getResources(), f, o);
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
int[] resized = ImageSize(new int[] { o.outWidth, o.outHeight }, width,
height);
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = calculateInSampleSize(o2, resized[0], resized[1]);
o2.inTempStorage = new byte[16 * 1024];
o = null;
o2 = null;
return BitmapFactory.decodeResource(getResources(), f, o2);
}
@Override
protected void onDestroy() {
super.onDestroy();
imageView1.setBackgroundResource(0);
imageView1 = null;
System.gc();
System.gc();
logHeap("ECAonDestroyonClick");
};
public static int[] ImageSize(int[] imgSize, int maxWidth, int maxHeight) {
int[] size = new int[2];
try {
for (double i = 1; i <= 20; i += 0.1) {
double height = imgSize[1] / i;
double width = imgSize[0] / i;
if (height < maxHeight && width < maxWidth) {
size[0] = (int) width;
size[1] = (int) height;
return size;
}
}
return imgSize;
} catch (Exception ex) {
return size;
}
}
public static void logHeap(String functionname) {
Double allocated = new Double(Debug.getNativeHeapAllocatedSize())
/ new Double((1048576));
Double available = new Double(Debug.getNativeHeapSize()) / 1048576.0;
Double free = new Double(Debug.getNativeHeapFreeSize()) / 1048576.0;
DecimalFormat df = new DecimalFormat();
df.setMaximumFractionDigits(2);
df.setMinimumFractionDigits(2);
Log.d("tag", "debug. =================================");
Log.d("tag", "debug.heap native: allocated " + df.format(allocated)
+ "MB of " + df.format(available) + "MB (" + df.format(free)
+ "MB free)");
Log.d("tag",
"functionname:"
+ functionname
+ ":debug.memory: allocated: "
+ df.format(new Double(Runtime.getRuntime()
.totalMemory() / 1048576))
+ "MB of "
+ df.format(new Double(
Runtime.getRuntime().maxMemory() / 1048576))
+ "MB ("
+ df.format(new Double(Runtime.getRuntime()
.freeMemory() / 1048576)) + "MB free)");
}
@Override
protected void onResume() {
super.onResume();
logHeap("ECAonResume");
};
public void onClick(View v) {
Intent a = new Intent(this, Activity2.class);
startActivity(a);
finish();
//imageView1.setBackgroundResource(0);
//imageView1 = null;
//System.gc();
//System.gc();
//logHeap("ECAonDestroyonClick");
}
}
在图片 1 堆大小没有减少:
您可以看到堆大小减小:
我面临一个奇怪的问题。
如果我从 onClick 事件中取消注释以下代码,我的堆内存被释放
// imageView1.setBackgroundResource(0);
// imageView1 = null;
// System.gc();
// System.gc();
// logHeap("ECAonDestroyonClick");
但是,如果我在 onDestory 事件中放置相同的代码,则没有任何效果。我在哪里犯错?处理图像的正确方法是什么?
java.lang.OutOfMemoryError: bitmap size exceeds VM budget.
当您尝试在程序中加载多个大型位图时,这是您在 android 中经常遇到的一个短语。为什么?因为应用程序可用的堆大小是有限的,而大位图是耗尽它的最简单方法。那么你如何解决它呢?