BitmapFactory.Options.inSampleSize 的用法

來源:互聯網
上載者:User

BitmapFactory.decodeFile(imageFile);

用BitmapFactory解碼一張圖片時,有時會遇到該錯誤。這往往是由於圖片過大造成的。要想正常使用,則需要分配更少的記憶體空間來儲存。

BitmapFactory.Options.inSampleSize

設定恰當的inSampleSize可以使BitmapFactory分配更少的空間以消除該錯誤。inSampleSize的具體含義請參考SDK文檔。例如:

BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = 4;
Bitmap bitmap = BitmapFactory.decodeFile(imageFile, opts);

設定恰當的inSampleSize是解決該問題的關鍵之一。BitmapFactory.Options提供了另一個成員inJustDecodeBounds。

BitmapFactory.Options opts = new BitmapFactory.Options();opts.inJustDecodeBounds = true;Bitmap bitmap = BitmapFactory.decodeFile(imageFile, opts);
   

設定inJustDecodeBounds為true後,decodeFile並不分配空間,但可計算出原始圖片的長度和寬度,即opts.width和opts.height。有了這兩個參數,再通過一定的演算法,即可得到一個恰當的inSampleSize。

查看Android源碼,我們得知,為了得到恰當的inSampleSize,Android提供了一種動態計算的方法。

public static int computeSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) {    int initialSize = computeInitialSampleSize(options, minSideLength, maxNumOfPixels);    int roundedSize;    if (initialSize <= 8) {        roundedSize = 1;        while (roundedSize < initialSize) {            roundedSize <<= 1;        }    } else {        roundedSize = (initialSize + 7) / 8 * 8;    }    return roundedSize;}private static int computeInitialSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) {    double w = options.outWidth;    double h = options.outHeight;    int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));    int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(Math.floor(w / minSideLength), Math.floor(h / minSideLength));    if (upperBound < lowerBound) {        // return the larger one when there is no overlapping zone.        return lowerBound;    }    if ((maxNumOfPixels == -1) && (minSideLength == -1)) {        return 1;    } else if (minSideLength == -1) {        return lowerBound;    } else {        return upperBound;    }} 使用該演算法,就可動態計算出圖片的inSampleSize。BitmapFactory.Options opts = new BitmapFactory.Options();opts.inJustDecodeBounds = true;BitmapFactory.decodeFile(imageFile, opts);opts.inSampleSize = computeSampleSize(opts, -1, 128*128);  opts.inJustDecodeBounds = false;try { Bitmap bmp = BitmapFactory.decodeFile(imageFile, opts); imageView.setImageBitmap(bmp);    } catch (OutOfMemoryError err) {    }




綜合上述,完整代碼是:

public static Bitmap createImageThumbnail(String filePath){ Bitmap bitmap = null; BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inJustDecodeBounds = true; BitmapFactory.decodeFile(filePath, opts); opts.inSampleSize = computeSampleSize(opts, -1, 128*128); opts.inJustDecodeBounds = false; try { bitmap = BitmapFactory.decodeFile(filePath, opts); }catch (Exception e) {// TODO: handle exception}return bitmap;}public static int computeSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) {    int initialSize = computeInitialSampleSize(options, minSideLength, maxNumOfPixels);    int roundedSize;    if (initialSize <= 8) {        roundedSize = 1;        while (roundedSize < initialSize) {            roundedSize <<= 1;        }    } else {        roundedSize = (initialSize + 7) / 8 * 8;    }    return roundedSize;}private static int computeInitialSampleSize(BitmapFactory.Options options,int minSideLength, int maxNumOfPixels) {    double w = options.outWidth;    double h = options.outHeight;    int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));    int upperBound = (minSideLength == -1) ? 128 :(int) Math.min(Math.floor(w / minSideLength), Math.floor(h / minSideLength));    if (upperBound < lowerBound) {        // return the larger one when there is no overlapping zone.        return lowerBound;    }    if ((maxNumOfPixels == -1) && (minSideLength == -1)) {        return 1;    } else if (minSideLength == -1) {        return lowerBound;    } else {        return upperBound;    }}

聯繫我們

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