我有一个间歇性的问题,从Camera活动捕捉图像,将其保存到一个较小的大小,并上传保存的图像到我的服务器。安卓 - OutOfMemoryError下采样大型相机图像
如果图像文件大于特定阈值(我用2,000KB)我打电话下面的函数下采样,并保存较小的图像大:
private void downsampleLargePhoto(Uri uri, int fileSizeKB)
{
int scaleFactor = (int) (fileSizeKB/fileSizeLimit);
log("image is " + scaleFactor + " times too large");
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
try
{
options.inJustDecodeBounds = false;
options.inSampleSize = scaleFactor;
Bitmap scaledBitmap = BitmapFactory.decodeStream(
getContentResolver().openInputStream(uri), null, options);
log("scaled bitmap has size " + scaledBitmap.getWidth() + " x " + scaledBitmap.getHeight());
String scaledFilename = uri.getPath();
log("save scaled image to file " + scaledFilename);
FileOutputStream out = new FileOutputStream(scaledFilename);
scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
scaledBitmap.recycle();
File image = new java.io.File(scaledFilename);
int newFileSize = (int) image.length()/1000;
log("scaled image file is size " + newFileSize + " KB");
}
catch(FileNotFoundException f)
{
log("FileNotFoundException: " + f);
}
}
有了非常大的图像,但是,我的应用程序崩溃与线上的OutOfMemoryError:
Bitmap scaledBitmap = BitmapFactory.decodeStream(
getContentResolver().openInputStream(uri), null, options);
我还能做些什么来缩小图像?
2013-03-28
TomBomb
+0
这已经在这里找到答案之前 [1]:http://stackoverflow.com/questions/477572/strange-out-of-memory-issue-while-loading-an-image-到一个位图对象/ 823966#823966 –