同事做android拍照,传输图片,经常出现内存溢出问题,造成app闪退。

提供如下解决方案。


import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.util.ArrayList;import android.content.Context;import android.graphics.Bitmap;import android.graphics.Bitmap.Config;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.Paint;import android.graphics.PorterDuff.Mode;import android.graphics.PorterDuffXfermode;import android.graphics.Rect;import android.graphics.RectF;public class BitmapUtils {public static ArrayList<Bitmap> id2Bitmap1(Context cxt, int[] imgId) {ArrayList<Bitmap> lists = new ArrayList<Bitmap>();Bitmap bmp = null;for (int i = 0; i < imgId.length; i++) {bmp = BitmapFactory.decodeStream(cxt.getResources().openRawResource(imgId[i]));lists.add(bmp);}return lists;}public static ArrayList<Bitmap> id2Bitmap(Context cxt, int[] imgId) {ArrayList<Bitmap> lists = new ArrayList<Bitmap>();Bitmap bmp = null;for (int i = 0; i < imgId.length; i++) {bmp = BitmapFactory.decodeStream(cxt.getResources().openRawResource(imgId[i]));lists.add(getRoundCornerBitmap(bmp, 6));}if (bmp != null) {bmp.recycle();}return lists;}public static boolean haveImage() {String path = Tools.getPath() + "imgs/";File file = new File(path);File[] files = file.listFiles();if (files.length != 0) {return true;}return false;}public static ArrayList<Bitmap> a(Context context, int zoomSize) {ArrayList<Bitmap> lists = new ArrayList<Bitmap>();String path = Tool.getPath() + "imgs/";Bitmap bitmap, bmp;BitmapFactory.Options opts = null;opts = new BitmapFactory.Options();opts.inJustDecodeBounds = true;BitmapFactory.decodeResource(context.getResources(), R.drawable.yz1,opts);opts.inSampleSize = zoomSize;opts.inJustDecodeBounds = false;opts.inInputShareable = true;opts.inPurgeable = true;File file = new File(path);File[] files = file.listFiles();if (files.length != 0) {for (int i = 0; i < files.length; i++) {File f = files[i];String fnm = f.getPath();try {bmp = BitmapFactory.decodeStream(new FileInputStream(f));bitmap = getRoundCornerBitmap(bmp, 8);lists.add(bitmap);} catch (FileNotFoundException e) {e.printStackTrace();}}return lists;} else {return null;}}public static ArrayList<String> b() {ArrayList<String> lists = new ArrayList<String>();String path = Tool.getPath() + "imgs/";File file = new File(path);File[] files = file.listFiles();for (int i = 0; i < files.length; i++) {File f = files[i];String name = f.getName();lists.add(name);}return lists;}public static Bitmap getRoundCornerBitmap(Bitmap bitmap, float roundPX) {int width = bitmap.getWidth();int height = bitmap.getHeight();Bitmap bitmap2 = Bitmap.createBitmap(width, height, Config.ARGB_8888);Canvas canvas = new Canvas(bitmap2);final int color = 0xff424242;final Paint paint = new Paint();final Rect rect = new Rect(0, 0, width, height);final RectF rectF = new RectF(rect);paint.setColor(color);paint.setAntiAlias(true);canvas.drawARGB(0, 0, 0, 0);canvas.drawRoundRect(rectF, roundPX, roundPX, paint);paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));canvas.drawBitmap(bitmap, rect, rect, paint);return bitmap2;}}



tools是一个获取sd卡路径的类。发现这样缩放就不会内存溢出闪退了。

更多相关文章

  1. Android开发学习笔记(十二) 获取系统可用内存
  2. android内存泄露
  3. Android原生(Native)C开发之二 framebuffer篇
  4. Android中图片占用内存的计算
  5. android性能之一:内存泄露、内存溢出的区别及原理分析
  6. Android(安卓)imageView图片按比例缩放(转)
  7. Android(安卓)Linux 内核介绍 (转)
  8. android中九宫图整齐排列(图片处理)
  9. Android多分辨率适配框架(3)— 使用指南

随机推荐

  1. sql 聚集索引和非聚集索引(详细整理)
  2. SQL SERVER数据库重建索引的方法
  3. SQL Server 压缩日志与减少SQL Server 文
  4. 针对Sqlserver大数据量插入速度慢或丢失
  5. ms sql server中实现的unix时间戳函数(含
  6. sql 判断函数、存储过程是否存在的代码整
  7. SQL SERVER 2000通讯管道后复用劫持
  8. SQL Server 2000安全配置详解
  9. SQL Server出现System.OutOfMemoryExcept
  10. SQL Server中检查字段的值是否为数字的方