android拍照造成内存泄露问题

同事做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卡路径的类。发现这样缩放就不会内存溢出闪退了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值