android端读取本地图片出现OutOfMemoryException
?}
?
?// 通过图片路径,宽度高度创建一个Bitmap对象
?private static Bitmap createBitmap(String path, int width, int height) {
??File file = new File(path);
??if (file.exists()) {
???InputStream in = null;
???try {
????in = new FileInputStream(file);
????Size size = getBitMapSize(path);
????if (size.equals(ZERO_SIZE)) {
?????return null;
????}
????int scale = 1;
????int a = size.getWidth() / width;
????int b = size.getHeight() / height;
????scale = Math.max(a, b);
????synchronized (OPTIONS_DECODE) {
?????OPTIONS_DECODE.inSampleSize = scale;
?????Bitmap bitMap = BitmapFactory.decodeStream(in, null,
???????OPTIONS_DECODE);
?????return bitMap;
????}
???} catch (FileNotFoundException e) {
??????????????? Log.v("BitMapUtil","createBitmap=="+e.toString());
???} finally {
????closeInputStream(in);
???}
??}
??return null;
?}
?
?// 关闭输入流
?private static void closeInputStream(InputStream in) {
??if (null != in) {
???try {
????in.close();
???} catch (IOException e) {
????Log.v("BitMapUtil","closeInputStream=="+e.toString());
???}
??}
?}
?
?// 图片大小
?static class Size {
??private int width, height;
??Size(int width, int height) {
???this.width = width;
???this.height = height;
??}
??public int getWidth() {
???return width;
??}
??public int getHeight() {
???return height;
??}
?}
?
?// 队列缓存参数对象
?static class QueueEntry {
??public String path;
??public int width;
??public int height;
?}
}
?
?? 在使用时我只调用了getBitmap方法,将需要设置的高度宽度以及本地图片路径传递过去就能自动返回bitmap给我,而且当捕捉到OOMError的时候将LinkedList的最后一张图片也就是最先存的图片进行溢出并回收就大功告成,特别注意的是这里捕捉错误Exception是获取不到的,一定要手动捕获OutOfMemoryError你才能进行处理(估计这些道理大家都懂得,所以不赘述啦,童鞋们加油!办法总比困难多o(∩_∩)o )