---
title: "Android 图片过大内存溢出解决方案"
author: "qjoycn"
date: "2012-09-25T17:04:00Z"
tags: []
slug: "android"
draft: false
lastmod: "2012-12-01T15:31:33Z"
id: 1739416
url: "https://qjoycn.iteye.com/blog/1739416"
---

我一般都是这样啦..

try  
{  
mBitmap=BitmapFactory.decodeFile(resPath);  
} catch (OutOfMemoryError e)  
{  
mBitmap=createFitinBitmap(resPath,SCREEWIDTH,SCREENHEIGHT);  
}

public Bitmap createFitinBitmap(String path, int fitinWidth, int fitinHeight)  
{  
Options opts = new Options();  
opts.inJustDecodeBounds = true;  
BitmapFactory.decodeFile(path, opts);  
int sampleSize1 = opts.outWidth / fitinWidth;  
int sampleSize2 = opts.outHeight / fitinHeight;  
opts.inSampleSize = sampleSize1&gt;sampleSize2? sampleSize1 : sampleSize2;  
opts.inJustDecodeBounds = false;  
opts.inDither = false;  
opts.inPreferredConfig = Bitmap.Config.RGB\_565;  
return BitmapFactory.decodeFile(path, opts);  
}