我想显示此图像并将其上传到服务器。
我正在使用相机应用程序拍照并将照片的文件路径返回到活动。我在某些手机上遇到内存不足错误,因为我尝试在设备之间导入恒定的图像大小。
如何在手机内存限制内工作的同时将最大的图像尺寸上传到服务器?
代码如下:
请求加载 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;
}
}