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圖像。謝謝! –