一.从文件或者资源中获取bitmap,为了尽可能的减少OOM的风险我们可以采取以下几个步骤
三.从网络中获取图片生成bitmap的方法。需要注意的是下载操作,无法在主线程中执行,需要另起一个新的线程。如果主线程需要等待该结果,可以用 子线程.join()来实现。
1.1 不获取bitmap内容,只获取bitmap的相关信息,这需要 设置
inJustDecodeBounds=true;
/*** 不获取 bitmap 本身,直接获取 bitmap 信息* @param res* @param rid* @return*/public static BitmapFactory.Options getBitmapInfo(Resources res,int rid){BitmapFactory.Options bop=new BitmapFactory.Options();bop.inJustDecodeBounds=true;BitmapFactory.decodeResource(res,rid,bop);int imageHeight=bop.outHeight;int imageWidth=bop.outWidth;return bop;}
1.2 计算最优的 inSampleSize
/*** 计算出最优的inSampleSize,用以对bitMap进行缩放* @param ops* @param reHeight* @param reWidth* @return*/public static int calculateInSampleSize(BitmapFactory.Options ops,int reWidth,int reHeight){int imageHeight=ops.outHeight;int imageWidth=ops.outWidth;int inSampleSize = 1;int heightRadio=imageHeight/2;int widthRadio=imageWidth/2;while(heightRadio/inSampleSize>reHeight&&widthRadio/inSampleSize>reWidth){inSampleSize*=2;}return inSampleSize;}
1.3 获取所需的bitmap
/*** 获取经过缩放的的bitmap* @param reHeight* @param reWidth* @return*/public static Bitmap getBitMap(Context context,int id,int reWidth,int reHeight){BitmapFactory.Options bop=getBitmapInfo(context.getResources(),id);if(reHeight!=0) {bop.inSampleSize = calculateInSampleSize(bop, reWidth, reHeight);}else{bop.inSampleSize = calculateInSampleSize(bop, reWidth);}bop.inJustDecodeBounds=false;Bitmap bitMap=BitmapFactory.decodeResource(context.getResources(), id, bop);Log.v("getBitMap==imageWidth", bop.outWidth + "");return bitMap;}
二 . 从view中获取bitmap,一般有两种方式
1. 第一种方法,相对较好,同样的情况下,较少的遇到返回为null的情况
/*** 从 view 中获取 bitMap* @param view* @return*/public static Bitmap loadBitmapFromView(View view,DisplayMetrics dm) {if (view == null) {return null;}Bitmap screenshot=null;view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());screenshot = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_4444);Canvas c = new Canvas(screenshot);view.draw(c);return screenshot;}
2. 第二种方法 ,很容易遇到返回为null的情况,主要原因是drawingCache的值很容易就大于系统给定的值。
/*** 从view中获取bitMap* @param view* @return*/public static Bitmap getBitmapFromView(View view){view.setDrawingCacheEnabled(true);view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());view.buildDrawingCache();Bitmap bitmap = view.getDrawingCache();return bitmap;}
public static byte[] getImage(String path) throws Exception {URL url = new URL(path);HttpURLConnection httpURLconnection = (HttpURLConnection)url.openConnection();httpURLconnection.setRequestMethod("GET");httpURLconnection.setReadTimeout(6*1000);httpURLconnection.setDoInput(true);InputStream in = null;byte[] b = new byte[1024];int len = -1;int resultcode=httpURLconnection.getResponseCode();if (resultcode == 200) {in = httpURLconnection.getInputStream();byte[] result = readStream(in);in.close();return result;
}return null;}
/*** 从网络获取bitmap* @param path* @param bop* @return*/public static Bitmap getBitmapByUrl(String path,BitmapFactory.Options bop){Bitmap bitMap=null;if(!path.startsWith("http")){bitMap = BitmapFactory.decodeFile(path,bop);return bitMap;}byte[] data = new byte[10];try {data = getImage(path);String d = new String(data);int length = data.length;bitMap = BitmapFactory.decodeByteArray(data, 0, length,bop);} catch (Exception e) {e.printStackTrace();}return bitMap;} 本文仅作为个人总结,参考(抄袭)了许许多多相关的博文,对作者们表示感谢。