Android載入大圖片(壓縮)

來源:互聯網
上載者:User

標籤:android   des   blog   http   java   get   

轉載自http://blog.csdn.net/junjx/article/details/7798604

在Android開發中,我們經常需要載入圖片。但是圖片的尺寸往往會很大,如果我們要的是比較小的圖片,在Android有限的記憶體下,我們顯然不能把大尺寸的圖片放到記憶體裡,這樣不但效率降低,而且會導致java.lang.OutOfMemory異常,相信很多朋友都遇到過這樣的問題,那麼該怎麼解決這一問題呢?其實在Android官方文檔中早已給出瞭解決的方案。

一、讀Bitmap的尺寸和類型

    BitmapFactory類提供一系列的方法 (decodeByteArray(),decodeFile(),decodeResource(), etc.)從資源中建立一個Bitmap。可以在這些方法中選擇適當的方法對圖片資源進行解碼。然而這些方法會很容易導致OutOfMemoryError異常。每種類型的解碼方法都有一些附加的屬性,你可以基於BitmapFactory.Options類提供的方法定義指定的解碼方式。

設定inJustDecodeBounds屬性為true,避免分配記憶體,返回一個null的Bitmap對象(包含outWidth,outHeight andoutMimeType),這樣就可以讀取圖片的尺寸和類型了。

 

 

[java] view plaincopy 
  1. BitmapFactory.Options options = new BitmapFactory.Options();  
  2. options.inJustDecodeBounds = true;  
  3. BitmapFactory.decodeResource(getResources(), R.id.myimage, options);  
  4. int imageHeight = options.outHeight;  
  5. int imageWidth = options.outWidth;  
  6. String imageType = options.outMimeType;  


二、載入一個縮小版到記憶體

 

現在我們已經擷取了圖片的大小,接下來我們該考慮是載入原圖到記憶體,還是載入一張縮圖到記憶體。這有幾點需要考慮的:

估算載入圖片佔用記憶體的大小

你打算在你的程式中給分出多少記憶體來載入圖片

你載入到目標元件圖表片的尺寸大小如何

當前裝置的螢幕的尺寸和密度大小

例如,我們要把一張1024x768像素的圖片載入到一個128x96像素的ImageView裡面,我們應該告訴解碼員(decoder)載入一張子圖到記憶體,這樣我們就需要設定BitmapFactory.Options對象的inSampleSize屬性,例如一張2048x1536解析度的圖片,如果設定inSampleSize=4,將會產生一張解析度為512x384的圖片,載入這張子圖到記憶體只需要消耗0.75MB的記憶體,而載入原圖卻需要12MB的記憶體(假設bitmap配置是ARGB_8888)。以下是代碼是根據圖片的大小及子圖的尺寸動態計算
imSampleSize的值的方法。

[java] view plaincopy 
  1. public static int calculateInSampleSize(  
  2.             BitmapFactory.Options options, int reqWidth, int reqHeight) {  
  3.     // Raw height and width of image  
  4.     final int height = options.outHeight;  
  5.     final int width = options.outWidth;  
  6.     int inSampleSize = 1;  
  7.   
  8.     if (height > reqHeight || width > reqWidth) {  
  9.         if (width > height) {  
  10.             inSampleSize = Math.round((float)height / (float)reqHeight);  
  11.         } else {  
  12.             inSampleSize = Math.round((float)width / (float)reqWidth);  
  13.         }  
  14.     }  
  15.     return inSampleSize;  
  16. }  


接著,首先設定inJustDecodeBounds=true,通過options設定inSampleSize的值再設定inJustDecodeBounds=false:

public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
        int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(res, resId, options);
}

這樣我們就能方便的得到一張解析度為100x100的子圖了:

mImageView.setImageBitmap(
    decodeSampledBitmapFromResource(getResources(), R.id.myimage, 100, 100));

你可以用類似的方法,根據你的需要採用其他的解碼方法來處理圖片。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.