今天測試程序的時候出現下面的錯誤日誌信息,程序當場掛掉html
07-09 14:11:25.434: W/System.err(4890): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
07-09 14:11:25.435: W/System.err(4890): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
07-09 14:11:25.435: W/System.err(4890): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:511)
07-09 14:11:25.435: W/System.err(4890): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:325)
07-09 14:11:25.435: W/System.err(4890): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:350)
使用Bitmap加載圖片的方法是這樣的java
vipAvatar = (ImageView) findViewById(R.id.vip_avatar); File file = new File(Environment.getExternalStorageDirectory() + IMAGE_FILE_NAME); if (file.exists()) { Bitmap bitmap = BitmapFactory.decodeFile(Environment .getExternalStorageDirectory() + IMAGE_FILE_NAME); Drawable drawable = new BitmapDrawable(bitmap); vipAvatar.setImageDrawable(drawable); }
在使用BitmapFactory.decodeFile的時候出錯了,查找緣由android
android系統限制,只給圖片分配8兆的內存,超過就崩,你圖片幾十KB,多是壓縮格式的,轉換成bitmap就是原本的大小。ide
增長VM的設置不太現實,若是是在模擬器上沒問題,可是考慮到真機使用,問題仍是要解決的。測試
這裏使用了一種方法,上述狀況沒有出現,方法是spa
在退出Activity時,將bitmap回收日誌
@Override protected void onDestroy() { if (bitmap != null && !bitmap.isRecycled()) bitmap.recycle(); super.onDestroy(); }
//////////////////////////////////////////////code
原創聲明 轉載請註明htm
本文出自 Ray-Ray的博客blog
文章地址 http://www.cnblogs.com/rayray/p/3180017.html
感謝你們的推薦和收藏
你的支持! 咱們的動力!