Android 拍照内存闪烁问题

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

提供如下解决方案。


  1. import java.io.File; 
  2. import java.io.FileInputStream; 
  3. import java.io.FileNotFoundException; 
  4. import java.util.ArrayList; 
  5. import android.content.Context; 
  6. import android.graphics.Bitmap; 
  7. import android.graphics.Bitmap.Config; 
  8. import android.graphics.BitmapFactory; 
  9. import android.graphics.Canvas; 
  10. import android.graphics.Paint; 
  11. import android.graphics.PorterDuff.Mode;
  12. import android.graphics.PorterDuffXfermode; 
  13. import android.graphics.Rect; 
  14. import android.graphics.RectF; 
  15.  
  16.  
  17. public class BitmapUtils { 
  18.  
  19.     public static ArrayList<Bitmap> id2Bitmap1(Context cxt, int[] imgId) { 
  20.         ArrayList<Bitmap> lists = new ArrayList<Bitmap>(); 
  21.         Bitmap bmp = null
  22.         for (int i = 0; i < imgId.length; i++) { 
  23.             bmp = BitmapFactory.decodeStream(cxt.getResources() 
  24.                     .openRawResource(imgId[i])); 
  25.             lists.add(bmp); 
  26.         } 
  27.         return lists; 
  28.     } 
  29.  
  30.     public static ArrayList<Bitmap> id2Bitmap(Context cxt, int[] imgId) { 
  31.         ArrayList<Bitmap> lists = new ArrayList<Bitmap>(); 
  32.         Bitmap bmp = null
  33.         for (int i = 0; i < imgId.length; i++) { 
  34.             bmp = BitmapFactory.decodeStream(cxt.getResources() 
  35.                     .openRawResource(imgId[i])); 
  36.             lists.add(getRoundCornerBitmap(bmp, 6)); 
  37.         } 
  38.         if (bmp != null) { 
  39.             bmp.recycle(); 
  40.         } 
  41.         return lists; 
  42.     } 
  43.  
  44.     public static boolean haveImage() { 
  45.         String path = Tools.getPath() + "imgs/"
  46.         File file = new File(path); 
  47.         File[] files = file.listFiles(); 
  48.         if (files.length != 0) { 
  49.             return true
  50.         } 
  51.         return false
  52.     } 
  53.  
  54.     public static ArrayList<Bitmap> a(Context context, int zoomSize) { 
  55.         ArrayList<Bitmap> lists = new ArrayList<Bitmap>(); 
  56.         String path = Tool.getPath() + "imgs/"
  57.         Bitmap bitmap, bmp; 
  58.         BitmapFactory.Options opts = null
  59.         opts = new BitmapFactory.Options(); 
  60.         opts.inJustDecodeBounds = true
  61.         BitmapFactory.decodeResource(context.getResources(), R.drawable.yz1, 
  62.                 opts); 
  63.         opts.inSampleSize = zoomSize; 
  64.         opts.inJustDecodeBounds = false
  65.         opts.inInputShareable = true
  66.         opts.inPurgeable = true
  67.         File file = new File(path); 
  68.         File[] files = file.listFiles(); 
  69.         if (files.length != 0) { 
  70.             for (int i = 0; i < files.length; i++) { 
  71.                 File f = files[i]; 
  72.                 String fnm = f.getPath(); 
  73.                 try
  74.                     bmp = BitmapFactory.decodeStream(new FileInputStream(f)); 
  75.                     bitmap = getRoundCornerBitmap(bmp, 8); 
  76.                     lists.add(bitmap); 
  77.                 } catch (FileNotFoundException e) { 
  78.                     e.printStackTrace(); 
  79.                 } 
  80.                  
  81.             } 
  82.             return lists; 
  83.         } else
  84.             return null
  85.         } 
  86.     } 
  87.  
  88.     public static ArrayList<String> b() { 
  89.         ArrayList<String> lists = new ArrayList<String>(); 
  90.         String path = Tool.getPath() + "imgs/"
  91.         File file = new File(path); 
  92.         File[] files = file.listFiles(); 
  93.         for (int i = 0; i < files.length; i++) { 
  94.             File f = files[i]; 
  95.             String name = f.getName(); 
  96.             lists.add(name); 
  97.         } 
  98.         return lists; 
  99.     } 
  100.  
  101.     public static Bitmap getRoundCornerBitmap(Bitmap bitmap, float roundPX) { 
  102.         int width = bitmap.getWidth(); 
  103.         int height = bitmap.getHeight(); 
  104.         Bitmap bitmap2 = Bitmap.createBitmap(width, height, Config.ARGB_8888); 
  105.         Canvas canvas = new Canvas(bitmap2); 
  106.         final int color = 0xff424242
  107.         final Paint paint = new Paint(); 
  108.         final Rect rect = new Rect(0, 0, width, height); 
  109.         final RectF rectF = new RectF(rect); 
  110.         paint.setColor(color); 
  111.         paint.setAntiAlias(true); 
  112.         canvas.drawARGB(0, 0, 0, 0); 
  113.         canvas.drawRoundRect(rectF, roundPX, roundPX, paint); 
  114.         paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); 
  115.         canvas.drawBitmap(bitmap, rect, rect, paint); 
  116.         return bitmap2; 
  117.     } 
  118.  
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卡路径的类。发现这样缩放就不会内存溢出闪退了
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值