android 低端机 图片采样,安卓 - OutOfMemoryError下采样大型相机图像

我有一个间歇性的问题,从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 –

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值