由于BitmapFactory.decodeFile()获取图片有时候会报内存溢出,用了bitmap.recycler()也没用,就用了以下方法来进行图片的
二次采样
BitmapFactory.Options options=new BitmapFactory.Options(); options.inJustDecodeBounds=true; BitmapFactory.decodeFile(outputImage.getAbsolutePath(),options); // WindowManager manager=getActivity().getWindowManager();//这方法不太准确 // int screenWidth=manager.getDefaultDisplay().getWidth(); // int screenHeight=manager.getDefaultDisplay().getHeight(); WindowManager manager=getWindowManager(); DisplayMetrics outMetrics=new DisplayMetrics(); manager.getDefaultDisplay().getMetrics(outMetrics); // int screenWidth=outMetrics.widthPixels;//获取手机屏幕的宽度和高度 // int screenHeight=outMetrics.heightPixels; int screenWidth=480; int screenHeight=800; int width=options.outWidth; int height=options.outHeight; int sampleSize=1; Log.e("text","screenWidth===="+screenWidth); Log.e("text","screenHeight==="+screenHeight); while((width/sampleSize)>screenWidth||(height/sampleSize)>screenHeight){ sampleSize*=2; } options.inSampleSize=sampleSize; options.inJustDecodeBounds=false; options.inPreferredConfig= Bitmap.Config.RGB_565; options.inDither=true; Bitmap bitmap=BitmapFactory.decodeFile(outputImage.getAbsolutePath(),options);