緊接上篇博文 , 在獲取拍照拍下的圖片並將其設爲背景的時候出現了問題spa
系統給出了這樣的警告:.net
Bitmap too large to be uploaded into a texture
意思很明顯:就是如今的手機硬件愈來愈強悍了(-.-||).拍下來的圖片太大,沒法直接展現.code
網上有說關閉硬件加速喝分片加載的辦法.不過這都不是我想要的.這種問題天然是要壓縮圖片的orm
嘛!!htm
壓縮代碼:blog
/** * 圖片質量壓縮 * **/ private Bitmap compressImage(Bitmap image) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); image.compress(Bitmap.CompressFormat.JPEG, 100, baos);//質量壓縮方法,這裏100表示不壓縮,把壓縮後的數據存放到baos中 int options = 100; while ( baos.toByteArray().length / 1024>100) { //循環判斷若是壓縮後圖片是否大於100kb,大於繼續壓縮 baos.reset();//重置baos即清空baos options -= 10;//每次都減小10 image.compress(Bitmap.CompressFormat.JPEG, options, baos);//這裏壓縮options%,把壓縮後的數據存放到baos中 } ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());//把壓縮後的數據baos存放到ByteArrayInputStream中 Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);//把ByteArrayInputStream數據生成圖片 return bitmap; } /** * 圖片比例壓縮 * */ private Bitmap getimage(String srcPath) { BitmapFactory.Options newOpts = new BitmapFactory.Options(); //開始讀入圖片,此時把options.inJustDecodeBounds 設回true了 newOpts.inJustDecodeBounds = true; Bitmap bitmap = BitmapFactory.decodeFile(srcPath,newOpts);//此時返回bm爲空 newOpts.inJustDecodeBounds = false; int w = newOpts.outWidth; int h = newOpts.outHeight; //如今主流手機比較可能是800*480分辨率,因此高和寬咱們設置爲 float hh = 800f;//這裏設置高度爲800f float ww = 480f;//這裏設置寬度爲480f //縮放比。因爲是固定比例縮放,只用高或者寬其中一個數據進行計算便可 int be = 1;//be=1表示不縮放 if (w > h && w > ww) {//若是寬度大的話根據寬度固定大小縮放 be = (int) (newOpts.outWidth / ww); } else if (w < h && h > hh) {//若是高度高的話根據寬度固定大小縮放 be = (int) (newOpts.outHeight / hh); } if (be <= 0) be = 1; newOpts.inSampleSize = be;//設置縮放比例 //從新讀入圖片,注意此時已經把options.inJustDecodeBounds 設回false了 bitmap = BitmapFactory.decodeFile(srcPath, newOpts); return compressImage(bitmap);//壓縮比如例大小後再進行質量壓縮 }
這是腳本之家的大神寫的拿來膜拜.圖片
大神連接:http://www.jb51.net/article/41117.htmget