同事做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;
}
}