Android BitmapFactory.decode文件内存不足,图像为2400x2400
我需要将一个图像从文件发送到服务器。服务器请求分辨率为2400x2400的图像 我想做的是: 1) 使用正确的inSampleSize,使用BitmapFactory.decode文件获取位图 2) 以JPEG格式压缩图像,质量为40% 3) 在base64中对图像进行编码 4) 发送到服务器 我无法完成第一步,它抛出内存不足异常。我确信inSampleSize是正确的,但我认为即使使用inSampleSize,位图也是巨大的(在DDMS中大约为30 MB) 你知道怎么做吗?我可以在不创建位图对象的情况下执行这些步骤吗?我的意思是在文件系统而不是RAM内存上执行 这是当前代码:Android BitmapFactory.decode文件内存不足,图像为2400x2400,android,bitmap,Android,Bitmap,我需要将一个图像从文件发送到服务器。服务器请求分辨率为2400x2400的图像 我想做的是: 1) 使用正确的inSampleSize,使用BitmapFactory.decode文件获取位图 2) 以JPEG格式压缩图像,质量为40% 3) 在base64中对图像进行编码 4) 发送到服务器 我无法完成第一步,它抛出内存不足异常。我确信inSampleSize是正确的,但我认为即使使用inSampleSize,位图也是巨大的(在DDMS中大约为30 MB) 你知道怎么做吗?我可以在不创建位图对
// The following function calculate the correct inSampleSize
Bitmap image = Util.decodeSampledBitmapFromFile(imagePath, width,height);
// compressing the image
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 40, baos);
// encode image
String encodedImage = Base64.encodeToString(baos.toByteArray(),Base64.DEFAULT));
public static 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) {
// Calculate ratios of height and width to requested height and width
final int heightRatio = Math.round((float) height / (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
// Choose the smallest ratio as inSampleSize value, this will guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
public static Bitmap decodeSampledBitmapFromFile(String path,int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(path,options);
}
//以下函数计算正确的inSampleSize
位图图像=Util.decodeSampledBitmapFromFile(图像路径、宽度、高度);
//压缩图像
ByteArrayOutputStream bas=新的ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG,40,baos);
//编码图像
字符串encodedImage=Base64.encodeToString(baos.toByteArray(),Base64.DEFAULT));
公共静态int-calculateInSampleSize(BitmapFactory.Options选项、int-reqWidth、int-reqHeight){
//图像的原始高度和宽度
最终内部高度=options.outHeight;
最终整数宽度=options.outWidth;
int inSampleSize=1;
如果(高度>要求高度| |宽度>要求宽度){
//计算高度和宽度与所需高度和宽度的比率
最终内部高度比=数学圆((浮动)高度/(浮动)要求高度);
最终整数宽度比=数学圆((浮动)宽度/(浮动)宽度);
//选择最小比率作为采样值,这将保证
//最终图像的两个尺寸均大于或等于
//要求的高度和宽度。
inSampleSize=高度比<宽度比?高度比:宽度比;
}
返回样本大小;
}
公共静态位图解码SampledBitMapFromFile(字符串路径、int-reqWidth、int-reqHeight){
//使用INJUSTDECBOUNDS首次解码=true检查尺寸
final BitmapFactory.Options=new BitmapFactory.Options();
options.inJustDecodeBounds=true;
解码文件(路径、选项);
//计算样本大小
options.inSampleSize=计算样本大小(options、reqWidth、reqHeight);
//使用inSampleSize集合解码位图
options.inJustDecodeBounds=false;
返回BitmapFactory.decodeFile(路径、选项);
}
必须使用BitmapFactory.Options
,并将inJustDecodeBounds
设置为true。通过这种方式,您可以加载有关位图的信息,并计算用于对其进行下采样的值(例如,inSampleSize
)不要将图像作为位图加载,将其转换为数组,然后发送它
相反:
以JPG格式将其作为文件读取。使用files字节数组对其进行编码,并跨多个字节发送文件
将其加载到位图会导致不必要的巨大内存问题。以位图格式重新呈现的图像将占用约20倍或更多的内存
在服务器端,您也需要将其视为一个文件。而不是位图
下面是将文件加载到字节[]的链接:您可以跳过ARGB_8888,改用RGB_565,然后抖动图像以保持良好的质量
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = false;
options.inPreferredConfig = Config.RGB_565;
options.inDither = true;
通过你的努力,你会对getBitmap()有所了解;你有一个文件里的图像,对吗?只需将此文件发送到服务器并在那里进行必要的转换。我无法将12MB的数据发送到服务器,对图像进行大小调整和压缩会大大减小图像的大小,不到1MBI希望看到它。你能用这段代码更新这篇文章吗?我需要一个位图,因为我需要调整它的大小并压缩它。原始图像大小为12MB,我希望发送到服务器的大小为700KB左右,而不是原始图像的12MB。@alamarr啊,我的错误。你一定是弄错了。请看@alasarr,让您知道。位图格式的
2400x2400
图像,每像素使用2字节
,大小11MB
。无论jpg格式是否为700KB
。或者在4字节
中,表示为22MB
它将位图的大小减小到13mb,并且可以正常工作。非常感谢你!