獲取程序運行時需要的內存
//獲得每個應用程序運行的時的最大內存
int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024/1024);
Log.i("info", "Max memory is " + maxMemory + "MB");
需要注意一下注意的幾個重要參數及方法
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), R.id.myimage, options);
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
String imageType = options.outMimeType;
1.首先是BitmapFactory.Options類,作用是允許我們定義圖片以何種方式如何讀到內存。
2.options.inJustDecodeBounds = true
官方文檔是這樣說明的
意思是說如果設置爲true那麼像BitmapFactory.decodeResource類似這樣的方法不會返回一個Bitmap對象而是null值,但是通過設置爲true我們可以得到原始圖片的寬高值,因而可以去計算inSampleSize的大小,看看Sdk文檔是怎麼進行描述的
public int inSampleSize
If set to a value > 1, requests the decoder to subsample the original image, returning a smaller image to save memory. The sample size is the number of pixels in either dimension that correspond to a single pixel in the decoded bitmap. For example, inSampleSize == 4 returns an image that is 1/4 the width/height of the original, and 1/16 the number of pixels. Any value <= 1 is treated the same as 1. Note: the decoder uses a final value based on powers of 2, any other value will be rounded down to the nearest power of 2.
可以將此參數理解爲對圖片壓縮的縮放比,如文檔所說的如果inSampleSize == 4,那麼將返回圖片原始寬高比的1/4,也就是將圖片大小壓縮了16倍。
(後面幾個參數的意思就比較簡單了,稍微說一下即可)
3.options.outHeight 圖片原始高度
4.options.outWidth 圖片原始寬度
5.options.outMimeType 圖片MimeType類型
注意outHeight 和outWight依賴於inJustDecodeBounds=true的設置
文檔如下:
方便以後查看,貼出所有Option的字段
親測2.43mb圖片會壓縮到273kb
測試源碼如下:
public class MainActivity extends Activity {
private ImageView iv;
private ImageView iv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//獲得每個應用程序運行的時的最大內存
int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024/1024);
Log.i("info", "Max memory is " + maxMemory + "MB");
iv = (ImageView) findViewById(R.id.iv);
Bitmap bitmap = decodeSampledBitmapFromResource(getResources(),
R.drawable.icon, 200, 200);
Log.i("info", "size:"+bitmap.getByteCount()/1024);
if(bitmap!=null)
iv.setImageBitmap(bitmap);
}
/**
* 計算壓縮比率
* @param options
* @param reqWidth
* @param reqHeight
* @return
*/
public int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
// 源圖片的高度和寬度
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
// 計算出實際寬高和目標寬高的比率
final int heightRatio = Math.round((float) height
/ (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
// 選擇寬和高中最小的比率作爲inSampleSize的值,這樣可以保證最終圖片的寬和高
// 一定都會大於等於目標的寬和高。
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
/**
* 得到壓縮後的bitmap實例
* @param res
* @param resId
* @param reqWidth
* @param reqHeight
* @return
*/
public Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) {
// 第一次解析將inJustDecodeBounds設置爲true,來獲取圖片大小
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// 調用上面定義的方法計算inSampleSize值
options.inSampleSize = calculateInSampleSize(options, reqWidth,
reqHeight);
// 使用獲取到的inSampleSize值再次解析圖片
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
}