Android Bitmap OutOfMemory

來源:互聯網
上載者:User

      在Android應用裡,最耗費記憶體的就是圖片資源。而且在Android系統中,讀取位元影像Bitmap時,分給虛擬機器中的圖片的堆棧大小隻有8M,如果超出了,就會出現OutOfMemory異常

E/AndroidRuntime(  697): java.lang.OutOfMemoryError
E/AndroidRuntime(  697):     at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
E/AndroidRuntime(  697):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:500)
E/AndroidRuntime(  697):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:353)
E/AndroidRuntime(  697):     at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:376)
E/AndroidRuntime(  697):     at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:406)
E/AndroidRuntime(  697):     at com.example.imagetoshow2.ImageAdapter.createReflectedImages(ImageAdapter.java:66)
E/AndroidRuntime(  697):     at com.example.imagetoshow2.ImageAdapter.getView(ImageAdapter.java:54)
E/AndroidRuntime(  697):     at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:193)

解決辦法:

1.及時回收記憶體

 if(bitmap != null && !bitmap.isRecycled()){         // 回收並且置為null        bitmap.recycle();         bitmap = null; } System.gc();
在適當的地方使用上述代碼,將暫時不需使用的的回收掉,當然system.gc不應該頻繁調用,否則會使系統效率降低。


2.使用BitmapFactory.Options對圖片進行壓縮

 BitmapFactory.Options opts = new BitmapFactory.Options();   opts.inSampleSize = n;           bitmap = BitmapFactory.decodeStream(fis, null, opts);

使用inSampleSize設定放縮比例,預設值為0,設定一個大於0的數便可對圖片進行壓縮。

 BitmapFactory.Options opts = new BitmapFactory.Options();    // 設定inJustDecodeBounds為true    opts.inJustDecodeBounds = true;    // 使用decodeFile方法得到圖片的寬和高    BitmapFactory.decodeFile(path, opts);

使inJustDecodeBounds為true後,再使用decodeFile()等方法,並不會真正的分配空間,即解碼出來的Bitmap為null,只會計算出options.outWidth和options.outHeight值,在下次使用BitmapFactory的decodeFile()等方法執行個體化Bitmap對象前,將opts.inJustDecodeBound設定回false就可以得到圖片了。


3.代碼最佳化

為了避免應用在分配Bitmap記憶體的時候出現OutOfMemory異常停止運行,通常,在執行個體化Bitmap的代碼中,對OutOfMemory異常進行捕獲

<span style="font-size:18px;"> <span style="font-size:18px;">Bitmap bitmap = null;try {    // 執行個體化Bitmap    bitmap = BitmapFactory.decodeFile(path);} catch (OutOfMemoryError e) {    //}</span></span>
然後在Catch部分做一些記憶體回收操作,或者是使用緩衝圖片等...

總是良好的編程風格和優質的代碼結構是程式員的無上追求....

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.