java.lang.OutOfMemoryError: Failed to allocate a 48771084 byte allocation with16771032 free bytes and29MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
atandroid.graphics.Bitmap.nativeCopy(Native Method)atandroid.graphics.Bitmap.copy(Bitmap.java:592)
// 调用代码// Bitmap bitmap = BitmapUtils.getScaledBitmapFromFile(graphicFile.getAbsolutePath(),// 1920, 1080);publicstatic Bitmap getScaledBitmapFromFile(String filePath, int requiredWidth, int requiredHeight) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);
options.inSampleSize = calculateInSampleSize(options, requiredWidth, requiredHeight);
options.inJustDecodeBounds = false;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
//don't use same inputstream object as in decodestream above. It will not work because//decode stream edit input stream. So if you create//InputStream is =url.openConnection().getInputStream(); and you use this in decodeStream//above and bellow it will not work!
Bitmap bm = BitmapFactory.decodeFile(filePath, options);
return bm;
}
/** 该方法即是计算图片缩放比例
* options 原图的参数
*/privatestaticintcalculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of imagefinalint height = options.outHeight;
finalint width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
finalint halfHeight = height / 2;
finalint halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both// height and width larger than the requested height and width.while ((halfHeight / inSampleSize) >= reqHeight
&& (halfWidth / inSampleSize) >= reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}