Utils.java如下:
package cn.loadImages;import java.io.InputStream;import java.io.OutputStream;import android.graphics.Bitmap;import android.graphics.BitmapFactory;public class Utils { public static void copyStream(InputStream is, OutputStream os){ final int buffer_size=1024; try{ byte[] bytes=new byte[buffer_size]; for(;;) { int count=is.read(bytes, 0, buffer_size); if(count==-1){ break; } os.write(bytes, 0, count); } } catch(Exception ex){} } //擷取圖片的縮圖public static Bitmap getBitmapThumbnail(String filePath,int minSideLength, int maxNumOfPixels ){ BitmapFactory.Options options=new BitmapFactory.Options(); //true那麼將不返回實際的bitmap對象 //不給其分配記憶體空間但是可以得到一些解碼邊界資訊即圖片大小等資訊 options.inJustDecodeBounds=true; //此時rawBitmap為nullBitmap rawBitmap = BitmapFactory.decodeFile(filePath, options);if (rawBitmap==null) {System.out.println("此時rawBitmap為null");}//inSampleSize表示縮圖大小為原始圖片大小的幾分之一,若該值為3//則取出的縮圖的寬和高都是原始圖片的1/3,圖片大小就為原始大小的1/9//計算sampleSizeint sampleSize=computeSampleSize(options, minSideLength, maxNumOfPixels);//為了讀到圖片,必須把options.inJustDecodeBounds設回falseoptions.inJustDecodeBounds = false;options.inSampleSize = sampleSize;//原圖大小為625x690 90.2kB //測試調用computeSampleSize(options, 100, 200*100);//得到sampleSize=8//得到寬和高位79和87//79*8=632 87*8=696 Bitmap thumbnailBitmap=BitmapFactory.decodeFile(filePath, options);return thumbnailBitmap;}//參考資料://http://my.csdn.net/zljk000/code/detail/18212//第一個參數:原本Bitmap的options//第二個參數:希望產生的縮圖的寬高中的較小的值//第三個參數:希望產生的縮量圖的總像素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;}}}