Android開發中 頁面加載一張超大圖片(561kb)時出現OOM

    今天作項目,發現須要顯示一張超大圖片,處理事後,還有561Kbjava

     加載的時候,就crash --- OOMandroid

     shortMsg:java.lang.OutOfMemoryError
函數

     longMsg:java.lang.OutOfMemoryError: bitmap size exceeds VM budget
this

     stackTrace:java.lang.OutOfMemoryError: bitmap size exceeds VM budget
     at android.graphics.Bitmap.nativeCreate(Native Method)
     at android.graphics.Bitmap.createBitmap(Bitmap.java:477)
     at android.graphics.Bitmap.createBitmap(Bitmap.java:444)
     at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:349)
     at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:512)
     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:487)
     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336)
spa

     

      代碼以下:.net

      detailView=(ImageView)findViewById(R.id.detailView);code

      detailView.setBackgroundResource(R.drawable.more_info);//this line will lead to OOM 
圖片

       換成這種:內存

      detailView.setImageResource(R.drawable.more_info); //也一樣會OOM資源

       

       後來找到了solution:

        /**
          * 以最省內存的方式讀取本地資源的圖片
          * @param context
          *@param resId
          * @return
          */  
    public static Bitmap readBitMap(Context context, int resId){  
        BitmapFactory.Options opt = new BitmapFactory.Options();  
        opt.inPreferredConfig = Bitmap.Config.RGB_565;   
       opt.inPurgeable = true;  
       opt.inInputShareable = true;  
          //獲取資源圖片  
       InputStream is = context.getResources().openRawResource(resId);  
           return BitmapFactory.decodeStream(is,null,opt);  
    }
     

    取得bitmap以後,再 detailView.setImageBitmap(pdfImage); 就ok了!      


     那是爲何,會致使oom呢:

         原來當使用像 imageView.setBackgroundResource,imageView.setImageResource, 或者 BitmapFactory.decodeResource  這樣的方法來設置一張大圖片的時候,

         這些函數在完成decode後,最終都是經過java層的createBitmap來完成的,須要消耗更多內存。

         所以,改用先經過BitmapFactory.decodeStream方法,建立出一個bitmap,再將其設爲ImageView的 source,decodeStream最大的祕密在於其直接調用JNI>>nativeDecodeAsset()來完成decode,無需再使用java層的createBitmap,從而節省了java層的空間。若是在讀取時加上圖片的Config參數,能夠跟有效減小加載的內存,從而跟有效阻止拋out of Memory異常。

        另外,須要特別注意: 

         decodeStream是直接讀取圖片資料的字節碼了, 不會根據機器的各類分辨率來自動適應,使用了decodeStream以後,須要在hdpi和mdpi,ldpi中配置相應的圖片資源,不然在不一樣分辨率機器上都是一樣大小(像素點數量),顯示出來的大小就不對了。

相關文章
相關標籤/搜索