android 类似于微博拍照上传,压缩图片,避免oom
来源:互联网 发布:nc.exe windows下载 编辑:程序博客网 时间:2024/05/18 00:05
1、压缩图片
/**
*压缩图片
**/
private Bitmap yasuo(Uri uri) { Bitmap bitmap = null; try { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; bitmap = BitmapFactory.decodeStream(this.getContentResolver() .openInputStream(uri), null, options); int picWidth = options.outWidth; int picHeight = options.outHeight; WindowManager windowManager = getWindowManager(); Display display = windowManager.getDefaultDisplay(); int screenWidth = display.getWidth(); int screenHeight = display.getHeight(); options.inSampleSize = 1; if (picWidth > picHeight) { if (picWidth > screenWidth) options.inSampleSize = picWidth / screenWidth; } else { if (picHeight > screenHeight) options.inSampleSize = picHeight / screenHeight; } options.inJustDecodeBounds = false; bitmap = BitmapFactory.decodeStream(this.getContentResolver() .openInputStream(uri), null, options); img_touxiang.setImageBitmap(bitmap); /* * if (bitmap.isRecycled() == false) { bitmap.recycle(); } */ System.gc(); } catch (Exception e1) { } return bitmap;}2、压缩图片
public static Bitmap scalePicture(String filename, int maxWidth,int maxHeight) { Bitmap bitmap = null; try {BitmapFactory.Options opts = new BitmapFactory.Options(); BitmapFactory.decodeFile(filename, opts); int srcWidth = opts.outWidth; int srcHeight = opts.outHeight; int desWidth = 0; int desHeight = 0; // 缩放比例 double ratio = 0.0; if (srcWidth > srcHeight) { ratio = srcWidth / maxWidth; desWidth = maxWidth; desHeight = (int) (srcHeight / ratio); } else { ratio = srcHeight / maxHeight; desHeight = maxHeight; desWidth = (int) (srcWidth / ratio); } // 设置输出宽度、高度 BitmapFactory.Options newOpts = new BitmapFactory.Options(); newOpts.inSampleSize = (int) (ratio) + 1; newOpts.inJustDecodeBounds = false; newOpts.outWidth = desWidth; newOpts.outHeight = desHeight; bitmap = BitmapFactory.decodeFile(filename, newOpts); } catch (Exception e) { // TODO: handle exception } return bitmap; }3、压缩图片
压缩图片质量:
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.
---------------------------------------------------------------------------------------------------------------------
内存溢出,你这么处理就可以。用完及时回收
BitmapFactory.Options options = new BitmapFactory.Options();
options.inTempStorage = new byte[16*1024];
Bitmap bitmapImage = BitmapFactory.decodeFile(path,opt);
(几M的照片或者不行)
-----------------------------------------------------------------------------------------------------------------------
Bitmap图片太大,会造成内存溢出。一般的都有图片预览机制,就是得到一张尺寸小一点的图片。
这里所谓的缩小尺寸可不是指在layout中设置一下宽高使图片缩小(其实质还是一张占内存大图),而是实实在在的将图片本身缩小,减小内存占用。
以下是方法,详细说明都在注释里了:
[java]
private Bitmap revitionImageSize(String path, int size) throws IOException {
// 取得图片
InputStream temp = this.getAssets().open(path);
BitmapFactory.Options options = new BitmapFactory.Options();
// 这个参数代表,不为bitmap分配内存空间,只记录一些该图片的信息(例如图片大小),说白了就是为了内存优化
options.inJustDecodeBounds = true;
// 通过创建图片的方式,取得options的内容(这里就是利用了java的地址传递来赋值)
BitmapFactory.decodeStream(temp, null, options);
// 关闭流
temp.close();
// 生成压缩的图片
int i = 0;
Bitmap bitmap = null;
while (true) {
// 这一步是根据要设置的大小,使宽和高都能满足
if ((options.outWidth >> i <= size)
&& (options.outHeight >> i <= size)) {
// 重新取得流,注意:这里一定要再次加载,不能二次使用之前的流!
temp = this.getAssets().open(path);
// 这个参数表示 新生成的图片为原始图片的几分之一。
options.inSampleSize = (int) Math.pow(2.0D, i);
// 这里之前设置为了true,所以要改为false,否则就创建不出图片
options.inJustDecodeBounds = false; www.2cto.com
bitmap = BitmapFactory.decodeStream(temp, null, options);
break;
}
i += 1;
}
return bitmap;
http://www.eoeandroid.com/thread-57731-1-1.html
http://blog.csdn.net/enkezhang/article/details/8259278
http://blog.163.com/gobby_1110/blog/static/29281715201210510525413/
- android 类似于微博拍照上传,压缩图片,避免oom
- Android中图片压缩技术以及图片缓存避免OOM
- 拍照上传图片的压缩
- android 拍照或选择相册图片压缩上传
- android 异步缓冲图片到内存 压缩图片 避免OOM 内存溢出
- Android 大图压缩处理,避免OOM
- Android 高清加载巨图方案 拒绝压缩图片 避免oom
- Android 中解决图片加载OOM 之 bitmap 压缩显示,压缩上传
- 图像压缩,避免OOM
- Android避免加载图片出现OOM
- Android加载图片,避免OOM的解决方案
- 【Android应用】加载图片避免oom
- 图片压缩设置为界面背景,避免OOM
- 图片压缩和缓存高效加载避免oom
- Android高效加载大图、多图解决方案,有效避免程序OOM (图片压缩和图片缓存LruCache)
- android图片压缩上传
- Android 图片压缩上传
- android 图片压缩上传
- STM32自带的CRC32软件计算
- 学习笔记_工厂方法模式
- J2EE中使用Filter过滤器清除网页缓存
- 中语言超酷特性 之 --- 中语言标准库
- 数学建模的初步认识
- android 类似于微博拍照上传,压缩图片,避免oom
- cocos2d-x项目生成.Apk并装机后gdb调试
- Python 代码性能优化技巧
- 哈夫曼树结合堆排序 POJ(3253)
- jquery validate(1) : 将校验规则写到控件中
- WPF编译器错误 CS1566 g.resources出错的终极解决
- 保护模式下 8259A芯片编程及中断处理探究(上)【1】
- C++ VC socket 文件下载 简单
- Features of C99