android 如何压缩图片

北漂雨田 2011-01-21 05:04:53
android 如何压缩图片,我用Gallery加载时图片时有一些图片太大,导致内存溢出,想把这些图片压缩成一个缩略图,该怎么做?
不能只是缩小图片大小,而是要把体积降下来,几百K的图片压缩成几K或几百B
...全文
4857 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
B111111N 2012-02-21
  • 打赏
  • 举报
回复
4楼的方法不行,对付小图片还可以,遇到那些6、7mb以上的图片就OOM 了。。
不信可以自己去测试
kallyena 2012-01-11
  • 打赏
  • 举报
回复
你有的Bitmap对象没有释放吧?
if (bitmap.isRecycled()){
bitmap.recycle();
}
SONAIS01 2011-10-27
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 anelka1985 的回复:]
内存溢出,你这么处理就可以。用完及时回收
BitmapFactory.Options options = new BitmapFactory.Options();

options.inTempStorage = new byte[16*1024];

Bitmap bitmapImage = BitmapFactory.decodeFile(path,opt);
[/Quote]

谢谢你的方法,不过我在左右滑动图片数次之后它还是出错了,
10-27 06:49:59.373: ERROR/AndroidRuntime(4841): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
不过大概滑动了二三十次才报错.
lds2008 2011-01-22
  • 打赏
  • 举报
回复
压缩图片质量:
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, fos);
其中的quality为0~100, 可以压缩图片质量, 不过对于大图必须对图片resize

这个是等比例缩放:
bitmap = Bitmap.createScaledBitmap(bitmap, width, height, false);

这个是截取图片某部分:
bitmap = Bitmap.createBitmap(bitmap, x, y, width, height);

这几个方法都是针对Bitmap的, 不过鉴于Bitmap可以从file中读取, 也可以写入file.

这是我知道Android自带库里中唯一可以缩放和压缩的图片方法.
Nico 2011-01-22
  • 打赏
  • 举报
回复
内存溢出,你这么处理就可以。用完及时回收
BitmapFactory.Options options = new BitmapFactory.Options();

options.inTempStorage = new byte[16*1024];

Bitmap bitmapImage = BitmapFactory.decodeFile(path,opt);
bigpretty 2011-01-22
  • 打赏
  • 举报
回复
可以对bitmap用距阵的方式缩小,很简单的,看看就明白了。
DrSmart 2011-01-21
  • 打赏
  • 举报
回复

80,359

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧