代码是参考参考谷歌的开发文档:http://developer.android.com/training/displaying-bitmaps/index.html
private class LoadBitmapTask extends AsyncTask<Integer, Void, Bitmap> {
@Override
protected Bitmap doInBackground(Integer... params) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(mImagePath, options);
options.inSampleSize = calculateInSampleSize(options, params[0],
params[1]);
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(mImagePath, options);
}
@Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
mImageView.setImageBitmap(result);
mImageView.initImageMatrix(mImageView.getWidth(),
mImageView.getHeight());
}
private int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
}