標籤:
一,下載
android下載大圖片(例如微博長圖片)會出現OOM down掉問題
解決這個問題的辦法是下載圖片時先得到圖片的寬度和高度,如果超出規定限制則對圖片進行縮放
關鍵參數
1. BitmapFactory.Options.inJustDecodeBounds
inJustDecodeBounds:boolean類型,如果設為true,則進行辯解判斷,並不申請bitmap記憶體
2.BitmapFactory.Options.inJustDecodeBounds.outWidth和outHeight
如果inJustDecodeBounds為true,則可得到outWidth和outHeight的值,根據這個兩個值決定是否進行縮放
eg
public static Drawable loadImage(String url){URL m;InputStream is = null;try {m = new URL(url);is = (InputStream) m.getContent();BufferedInputStream bis = new BufferedInputStream(is);//標記其實位置,供reset參考bis.mark(0);BitmapFactory.Options opts = new BitmapFactory.Options();//true,只是讀圖片大小,不申請bitmap記憶體opts.inJustDecodeBounds = true;BitmapFactory.decodeStream(bis, null, opts);Log.v("AsyncImageLoader", "width="+opts.outWidth+"; height="+opts.outHeight);int size = (opts.outWidth * opts.outHeight);if( size > 1024*1024*4){int zoomRate = 2; //zommRate縮放比,根據情況自行設定,如果為2則縮放為原來的1/2,如果為1不縮放if(zoomRate <= 0) zoomRate = 1;opts.inSampleSize = zoomRate;Log.v("AsyncImageLoader", "圖片過大,被縮放 1/"+zoomRate);}//設為false,這次不是預讀取圖片大小,而是返回申請記憶體,bitmap資料opts.inJustDecodeBounds = false;//緩衝輸入資料流定位至頭部,mark()bis.reset();Bitmap bm = BitmapFactory.decodeStream(bis, null, opts); is.close(); bis.close();return (bm == null) ? null : new BitmapDrawable(bm);} catch (MalformedURLException e1) {Log.v("AsyncImageLoader", "MalformedURLException");e1.printStackTrace();} catch (IOException e) {Log.v("AsyncImageLoader", "IOException");e.printStackTrace();}return null;}
二、載入
Android應用開發中我們會經常用到圖片處理的技術
移動開發中,記憶體資源很寶貴,而且對載入圖片記憶體空間也有限制;所以我們會在載入圖片對圖片進行相應的處理,有時為了提高響應速度,增強使用者體驗, 我們在載入大圖片時會先載入圖片的縮圖、如後載入原圖,所以我們要將圖片按照固定大小取縮圖,一般取縮圖的方法是使用BitmapFactory的 decodeFile方法,然後通過傳遞進去 BitmapFactory.Option類型的參數進行取縮圖,在Option中,屬性值inSampleSize表示縮圖大小為原始圖片大小的幾 分之一,即如果這個值為2,則取出的縮圖的寬和高都是原始圖片的1/2,圖片大小就為原始大小的1/4。
然而,如果我們想取固定大小的縮圖就比較困難了,比如,我們想將不同大小的圖片去出來的縮圖高度都為200px,而且要保證圖片不失真,那怎麼 辦?我們總不能將原始圖片載入到記憶體中再進行縮放處理吧,要知道在移動開發中,記憶體是相當寶貴的,而且一張100K的圖片,載入完所佔用的記憶體何止 100K?
經過研究,發現,Options中有個屬性inJustDecodeBounds,研究了一下,終於明白是什麼意思了,SDK中的E文是這麼說的
If set to true, the decoder will return null (no bitmap), but the out... fields will still be set, allowing the caller to query the bitmap without having to allocate the memory for its pixels.
意思就是說如果該值設為true那麼將不返回實際的bitmap不給其分配記憶體空間而裡面只包括一些解碼邊界資訊即圖片大小資訊,那麼相應的方法也就出來了,通過設定inJustDecodeBounds為true,擷取到outHeight(圖片原始高度)和 outWidth(圖片的原始寬度),然後計算一個inSampleSize(縮放值),然後就可以取圖片了,這裡要注意的是,inSampleSize 可能小於0,必須做判斷。
具體代碼如下:
FrameLayout fr=(FrameLayout)findViewById(R.id.FrameLayout01);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
// 擷取這個圖片的寬和高
Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/test.jpg", options); //此時返回bm為空白
options.inJustDecodeBounds = false;
//計算縮放比
int be = (int)(options.outHeight / (float)200);
if (be <= 0)
be = 1;
options.inSampleSize = be;
//重新讀入圖片,注意這次要把options.inJustDecodeBounds 設為 false哦
bitmap=BitmapFactory.decodeFile("/sdcard/test.jpg",options);
int w = bitmap.getWidth();
int h = bitmap.getHeight();
System.out.println(w+" "+h);
ImageView iv=new ImageView(this);
iv.setImageBitmap(bitmap);
這樣我們就可以讀取較大的圖片就會避免記憶體溢出了。如果你想把壓縮後的圖片儲存在Sdcard上的話就很簡單了:
File file=new File("/sdcard/feng.png");
try {
FileOutputStream out=new FileOutputStream(file);
if(bitmap.compress(Bitmap.CompressFormat.PNG, 100, out)){
out.flush();
out.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
ok,這樣就把圖片儲存在/sdcard/feng.png這個檔案裡面了
android 應用開發對大圖片的處理