我使用我在Handler线程上打开的自定义相机来拍摄我的jpeg回调。
我希望在缩小图片后将图片存储在缓存中,以便图片适合1000 x 1000尺寸保持其比例。
为此,我使用以下方式从jpeg回调中拍摄照片:
private Camera.PictureCallback jpegCallback = new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, final Camera camera) {
Bitmap realImage;
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
options.inPurgeable=true;
options.inInputShareable=true;
realImage = BitmapFactory.decodeByteArray(data,0,data.length,options);
}
到目前为止,我可以拍摄尽可能多的照片,没有记忆问题...
但是当我想将图片写入缓存时,我需要使用以下方式缩小图片:
private class addThumbnailASYNC extends AsyncTask<Bitmap, Void, Bitmap> {
@Override
protected Bitmap doInBackground(Bitmap... bitmap) {
Bitmap image = bitmap[0];
int width = image.getWidth();
int height = image.getHeight();
float bitmapRatio = (float)width / (float) height;
if (bitmapRatio > 0) {
width = MAX_SIZE_IMAGE;
height = (int) (width / bitmapRatio);
} else {
height = MAX_SIZE_IMAGE;
width = (int) (height * bitmapRatio);
}
image = Bitmap.createScaledBitmap(image, width, height, true);
}
问题出在我使用的时候
image = Bitmap.createScaledBitmap
我得到一个Grow堆,如果我拍很多照片,我认为会导致内存不足。
有三件事我不明白:
options.inSampleSize
时,图片应缩放4,那么为什么createScaledBitmap会导致增长堆? createScaledBitmap
并且我将图像写入缓存,我从来没有得到增长堆为什么? inSampleSize
应该给我比使用scaledBitmap创建的文件更重的文件?