0
我想显示此图像并将其上载到服务器。内存不足错误 - 位图大小
我正在使用相机应用拍摄照片并将照片的文件路径返回到活动。我在某些手机上遇到了“内存不足”错误,因为我尝试在设备之间导入一个不变的图像大小。
在手机的内存限制内仍能工作时,如何才能将最大图像大小上传到服务器?
代码如下:
请求加载Aync
GetBitmapTask GBT = new GetBitmapTask(dataType, path, 1920, 1920, loader);
GBT.addAsyncTaskListener(new AsyncTaskDone()
{
@Override
public void loaded(Object resp)
{
crop.setImageBitmap((Bitmap)resp);
crop.setScaleType(ScaleType.MATRIX);
}
@Override
public void error() {
}
});
GBT.execute();
的异步任务是抛出OOM错误
public class GetBitmapTask extends AsyncTask<Void, Integer, Bitmap>
{
...
@Override
public Bitmap doInBackground(Void... params)
{
Bitmap r = null;
if (_dataType.equals("Unkown"))
{
Logger.e(getClass().getName(), "Error: Unkown File Type");
return null;
}
else if (_dataType.equals("File"))
{
Options options = new Options();
options.inJustDecodeBounds = true;
//Logger.i(getClass().getSimpleName(), _path.substring(7));
BitmapFactory.decodeFile(_path.substring(7), options);
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
Logger.i(getClass().getSimpleName(),
"height: " + options.outHeight +
"\nwidth: " + options.outWidth +
"\nmimetype: " + options.outMimeType +
"\nsample size: " + options.inSampleSize);
options.inJustDecodeBounds = false;
r = BitmapFactory.decodeFile(_path.substring(7), options);
}
else if (_dataType.equals("Http"))
{
r = _loader.downloadBitmap(_path, reqHeight);
Logger.i(getClass().getSimpleName(), "height: " + r.getHeight() +
"\nwidth: " + r.getWidth());
}
return r;
}
public static int calculateInSampleSize(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;
while (height/inSampleSize > reqHeight || width/inSampleSize > reqWidth)
{
if (height > width)
{
inSampleSize = height/reqHeight;
if (((double)height % (double)reqHeight) != 0)
{
inSampleSize++;
}
}
else
{
inSampleSize = width/reqWidth;
if (((double)width % (double)reqWidth) != 0)
{
inSampleSize++;
}
}
}
return inSampleSize;
}
}
我最终发现错误在代码中比位图任务更深,但是您非常正确,我不需要显示1920X1920图像。谢谢! –