相册获取图片报OOM异常解决办法
2017-01-13 17:02
351 查看
获取一个图片容易报OOM异常,这里做个笔记,但是这个会把图片搞得很模糊,图片的质量有所下降,后续会完善
private void setPic(ImageView imageView, Uri uri) { if (getActivity() != null) { //获取目标控件的大小 int targetW = imageView.getWidth(); int targetH = imageView.getHeight(); BitmapFactory.Options bmOptions = new BitmapFactory.Options(); try { //inJustDecodeBounds为true,可以加载源图片的尺寸大小,decodeStream方法返回的bitmap为null bmOptions.inJustDecodeBounds = true; BitmapFactory.decodeStream(getActivity().getContentResolver().openInputStream(uri), null, bmOptions); // 得到源图片的尺寸 int photoW = bmOptions.outWidth; int photoH = bmOptions.outHeight; //通过比较获取较小的缩放比列 int scaleFactor = Math.min(photoW / targetW, photoH / targetH); // 将inJustDecodeBounds置为false,设置bitmap的缩放比列 bmOptions.inJustDecodeBounds = false; bmOptions.inSampleSize = scaleFactor; bmOptions.inPurgeable = true; //再次decode获取bitmap Bitmap bitmap = BitmapFactory.decodeStream(getActivity().getContentResolver().openInputStream(uri), null, bmOptions); imageView.setImageBitmap(bitmap); } catch (FileNotFoundException e) { e.printStackTrace(); } } }
相关文章推荐
- 根据两点经纬度求直线与正北方向的夹角
- 《笨办法学python(第三版)》习题1
- java hashCode() equals()
- jQuery学习之jQuery Ajax用法详解
- String使用拼接对性能的影响和原因。
- 学习笔记:gzip + boost::iostream
- MySql按周,按月,按日分组统计数据
- 虚析构函数
- linux命令:httpd 安装配置及网页虚拟主机 elinks htpasswd
- Time_wait问题小结
- Naive and Silly Muggles
- doT.js灵活运用之嵌入使用
- iOS利用UIDocumentInteractionController和Quick Look打开或预览文档
- MySQL索引原理及慢查询优化
- linux 重定向 管道
- PHP 编译安装共享PECL扩展库
- linux开机服务启动顺序详解
- 在Windows7上创建WiFi热点共享有线网络连接
- 计算两个经纬度的距离(米)
- 【POJ3180】The Cow Prom奶牛舞会 还是一道挺裸的有向图强连通分量