How to reduce memory consumption in android
本问题已经有最佳答案,请猛点这里访问。
Possible Duplicate:
Android: Strange out of memory issue while loading an image to a Bitmap object
我是android领域的新手。 我不知道如何减少android中的内存消耗。 在我的应用程序中,从Web绘制大量图像并显示到网格视图中。 运行应用程序时"出现内存问题"。
请帮我
1)缩小尺寸并缩小图像尺寸
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | /** * decodes image and scales it to reduce memory consumption * * @param file * @param requiredSize * @return */ public static Bitmap decodeFile(File file, int requiredSize) { try { // Decode image size BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds = true; BitmapFactory.decodeStream(new FileInputStream(file), null, o); // The new size we want to scale to // Find the correct scale value. It should be the power of 2. int width_tmp = o.outWidth, height_tmp = o.outHeight; int scale = 1; while (true) { if (width_tmp / 2 < requiredSize || height_tmp / 2 < requiredSize) break; width_tmp /= 2; height_tmp /= 2; scale *= 2; } // Decode with inSampleSize BitmapFactory.Options o2 = new BitmapFactory.Options(); o2.inSampleSize = scale; Bitmap bmp = BitmapFactory.decodeStream(new FileInputStream(file), null, o2); return bmp; } catch (FileNotFoundException e) { } finally { } return null; } |
2)使用
3)使用