您好,登錄后才能下訂單哦!
在Android經(jīng)常使用到Bitmap用于顯示圖片,如果圖片過(guò)大,容易出現(xiàn)"OutOfMemory"異常,所以要對(duì)圖片進(jìn)行壓縮顯示。
通常使用BitmapFactory類的幾個(gè)方法(decodeByteArray(), decodeFile(), decodeResource()等)來(lái)建立一個(gè)bitmap,在生成bitmap前,可以通過(guò)BitmapFactory.Options來(lái)設(shè)置屬性,來(lái)保證不會(huì)出現(xiàn)OutOfMemory異常。首先可以通過(guò)需要顯示圖片的長(zhǎng)寬來(lái)獲取縮小的倍數(shù):
private int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { // Raw height and width of p_w_picpath final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { if (width > height) { inSampleSize = Math.round((float) height / (float) reqHeight); } else { inSampleSize = Math.round((float) width / (float) reqWidth); } } return inSampleSize; }
PS:官方文檔說(shuō)到,圖片壓縮時(shí),使用2的倍數(shù)壓縮效率會(huì)高,就是2,4,8…這種,我這里使用的是更接近需要的壓縮倍數(shù),官方文檔看這里。
使用兩種方式來(lái)壓縮圖片,一種是根據(jù)需要的圖片長(zhǎng)寬,一種是根據(jù)需要的圖片大?。ň褪嵌嗌貹)。
先看第一種:
public Bitmap GetThumbImageByWH(boolean isRound,String imgPath, int p_w_picpathwidth, int p_w_picpathheight) { try { File picture = new File(imgPath); BitmapFactory.Options bitmapFactoryOptions = new BitmapFactory.Options(); // set height and width of p_w_picpath bitmapFactoryOptions.inJustDecodeBounds = true; Bitmap bmap = BitmapFactory.decodeFile(picture.getAbsolutePath(), bitmapFactoryOptions); int inSampleSize = calculateInSampleSize(bitmapFactoryOptions,p_w_picpathwidth,p_w_picpathheight); bitmapFactoryOptions.inSampleSize = inSampleSize; bitmapFactoryOptions.inJustDecodeBounds = false; bmap = BitmapFactory.decodeFile(picture.getAbsolutePath(), bitmapFactoryOptions); return bmap; } catch (Exception e) { e.printStackTrace(); return null; } }
PS:如果使用一個(gè)BitmapFactory.Options對(duì)象,要先把該對(duì)象的inJustDecodeBounds屬性設(shè)置為true,inSampleSize設(shè)置完成后再設(shè)置為false。后面的是用來(lái)翻轉(zhuǎn)圖片的。
第二種方式:
public Bitmap getThumbImageBySize(String imgpath, int size, boolean adjustOrientation) { File file=new File(imgpath); FileInputStream fis = null; int filesize=0; try{ fis = new FileInputStream(file); filesize = fis.available(); Log.v("file length", ""+filesize); fis.close(); }catch(Exception ex){ Log.v("Read file error", ""+ex.getMessage()); } if(filesize/1024<size){ return BitmapFactory.decodeFile(imgpath); } // Revision: BitmapFactory.Options options = new BitmapFactory.Options(); // Set it false to not build the bitmap,just record its width and height options.inJustDecodeBounds = true; // Get the Options object by the path BitmapFactory.decodeFile(imgpath, options); int height = options.outHeight; int width = options.outWidth; Bitmap smallBitmap = null; double multiple = (float)(width*height*4)/(float)(size*1024); int inSampleSize = (int)Math.ceil(Math.sqrt(((float)filesize/1024.0)/(float)size)); options.inSampleSize=inSampleSize; options.inJustDecodeBounds = false; smallBitmap = BitmapFactory.decodeFile(imgpath, options); return smallBitmap; } }
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。