Android 图片压缩展示 防止使用MediaStore.Images.Media.getBitmap造成的OOM异常
来源:互联网 发布:worktile for mac 编辑:程序博客网 时间:2024/05/22 04:33
从上篇文章中我们知道裁剪之后得到的是bitmap,这时候我们直接用图片加载控件使用setImageBitmap加载bitmap就行了。然而现在我不想剪切了,就在相册中直接选取加载怎么办,我们调用相册或摄像头拍照获取到的结果是一个本地图片的uri,我们怎么加载这一个uri呢?
方式一:
直接使用imageView.setImageURI(uri);
但是这种方式不稳定,经常会报错,所以一般不用。
方式二:
把uri变成bitmap加载
Bitmap 和 uri 互变方法:
1、bitmap to uri
Uri uri = Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, null,null));
2、uri to bitmap
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
会出现的问题:
使用MediaStore.Images.Media.getBitmap把uri变成bitmap,加载多张图片会造成的OOM异常
原因:
getBitmap是一个简单粗暴的方法,我们没有要得到的bitmap的大小,所以返回的bitmap的大小我们不知道多少,完全由要加载的本地图片的大小决定,而现在的本地拍摄的图片比较大,所以加载四五张之后就可能会造成OOM溢出,所以我们要对bitmap展示前进行压缩。
getBitmap的源码:
public static final Bitmap getBitmap(ContentResolver cr, Uri url) throws FileNotFoundException, IOException { InputStream input = cr.openInputStream(url); Bitmap bitmap = BitmapFactory.decodeStream(input); input.close(); return bitmap;}
压缩方法:
public String getPath(Uri uri) { String[] projection = {MediaStore.Images.Media.DATA }; Cursor cursor = managedQuery(uri, projection, null, null, null); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return cursor.getString(column_index); } public final static Bitmap lessenUriImage(String path){ BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; Bitmap bitmap = BitmapFactory.decodeFile(path, options); //此时返回 bm 为空 options.inJustDecodeBounds = false; //缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可int be = (int)(options.outHeight / (float)320); if (be <= 0) be = 1;options.inSampleSize = be; //重新读入图片,注意此时已经把 options.inJustDecodeBounds 设回 false 了 bitmap=BitmapFactory.decodeFile(path,options); int w = bitmap.getWidth(); int h = bitmap.getHeight(); System.out.println(w+" "+h); //after zoomreturn bitmap;}
这样,我们就能流畅的加载本地图片了。
版权所有,转载请注明出处!
- Android 图片压缩展示 防止使用MediaStore.Images.Media.getBitmap造成的OOM异常
- 使用 MediaStore.Images.Media.getBitmap从Uri中获得bitmap以及其缺陷
- 【Android】调用MediaStore.Images.Media.insertImage保存图片时生成两张图片的问题
- MediaStore.Images.Media.insertImage 得到保存图片的原始路径
- MediaStore.Images.Media.insertImage
- 高效展示图片,防止OOM
- Bitmap的使用 防止OOM异常
- 比较靠谱的Android图片压缩处理机制,防止OOM
- android图片的压缩处理,防止出现OOM(out of memory)
- 比较靠谱的Android图片压缩处理机制,防止OOM
- android 加载图片防止oom的处理
- android 压缩图片大小,防止OOM
- android 压缩图片大小,防止OOM
- MediaStore.Images.Media.insertImage 参数解读
- Android之解决太大太多图片造成的oom
- Android之解决太大太多图片造成的oom
- Android之解决太大太多图片造成的oom
- 使用LruCache缓存,轻松解决图片过多造成的OOM
- springMVC请求访问静态资源报错javax.servlet.http.HttpServletRequest.getHeader(Ljava/lang/String;)
- hdoj 2041/2042/2043/2044/2046 (递归——斐波那契数列)
- 句柄
- 设计模式:策略模式(Strategy)
- php编码解码json对象
- Android 图片压缩展示 防止使用MediaStore.Images.Media.getBitmap造成的OOM异常
- 常见的几种RuntimeException
- Java 8 新特性:Lambda 表达式之方法引用(Lambda 表达式补充版)——诺诺"涂鸦"记忆
- monodb因 /usr/lib64/libssl.so.10: no version information available原因不能连接的问题
- org.apache.commons.net.io.CopyStreamException: IOException caught while copying
- 网络:监听网络(使用Reachability框架)
- 88.NSUserDefaults数据存储总结
- Log工具类
- 网络:ASI 框架使用(上传与下载文件)