1. 程式人生 > >常用方法(2)------根據圖片的url路徑獲得Bitmap物件

常用方法(2)------根據圖片的url路徑獲得Bitmap物件

coding過程中會遇到將本地的圖片的物理路徑轉化為Bitmap,可使用下面的方法:

/**
     * @param path:圖片的物理路徑
     * @param display_width:設定圖片的寬
     * @param display_height:設定圖片的高
     * @return
     * @throws Exception
     */
    private Bitmap decodeBitmap(String path,float display_width,float display_height) throws Exception {
        if
(Float.compare(display_width,0.0f) == 0 || Float.compare(display_width,0.0f) == 0) { throw new Exception("圖片的寬高均不能為0!"); } BitmapFactory.Options op = new BitmapFactory.Options(); //inJustDecodeBounds //If set to true, the decoder will return null (no bitmap), but the out…
op.inJustDecodeBounds = true; Bitmap bmp = BitmapFactory.decodeFile(path, op); //獲取尺寸資訊 //獲取比例大小 int wRatio = (int)Math.ceil(op.outWidth/display_width); int hRatio = (int)Math.ceil(op.outHeight/display_height); //如果超出指定大小,則縮小相應的比例 if(wRatio > 1 && hRatio > 1
){ if(wRatio > hRatio){ op.inSampleSize = wRatio; }else{ op.inSampleSize = hRatio; } } op.inJustDecodeBounds = false; bmp = BitmapFactory.decodeFile(path, op); return bmp; }