调用系统相册报out of memory 错误的解决办法
来源:互联网 发布:ac尼尔森数据 滑板 编辑:程序博客网 时间:2024/06/05 05:47
调用系统相册,用户选择图片后,将图片取出显示到imageview中。但是实际真机测试时发现有的图片正常返回,有的会报out of Memory 错误。错误详细是Biemap.Factory.decode错误,通过在网上搜索答案和自己实践,找到了解决办法。
之前调用系统相册的代码
ContentResolver resolver = getContentResolver();Uri originalUri = data.getData(); //得到返回的Uri bitmap = MediaStore.Images.Media.getBitmap(resolver,originalUri); //直接取出图片,会报错 this.imageView.setImageBitmap(smallBitmap);
改进后的代码
ContentResolver resolver = getContentResolver(); Uri originalUri = data.getData(); String path=getFilePathFromContentUri(originalUri,resolver); //把uri 转换成path,后面才可以调用decodeFile
BitmapFactory.Options options =new BitmapFactory.Options(); options.inJustDecodeBounds=true; bitmap=BitmapFactory.decodeFile(path, options); options.inJustDecodeBounds=false; int be=(int)(options.outHeight/(float)200); if(be<=0) be=1; options.inSampleSize = be; //设定缩放比例 bitmap=BitmapFactory.decodeFile(path, options); int w=bitmap.getWidth(); int h=bitmap.getHeight(); System.out.println(w+" "+h); bitmap= Bitmap.createScaledBitmap(bitmap, 350, 250, true); //使显示出来的bitmap都是固定大小 this.imageView.setImageBitmap(bitmap);
public int inSampleSize
If set to a value > 1, requests the decoder to subsample the original image, returning a smaller image to save memory. The sample size is the number of pixels in either dimension that correspond to a single pixel in the decoded bitmap. For example, inSampleSize == 4 returns an image that is 1/4 the width/height of the original, and 1/16 the number of pixels. Any value <= 1 is treated the same as 1. Note: the decoder uses a final value based on powers of 2, any other value will be rounded down to the nearest power of 2.
public boolean inJustDecodeBounds
If set to true, the decoder will return null (no bitmap), but the out... fields will still be set, allowing the caller to query the bitmap without having to allocate the memory for its pixels.
- 调用系统相册报out of memory 错误的解决办法
- 关于SetLength报Out of memory的研究及解决办法
- win7下Matlab中out of memory错误的解决办法
- 运行Ant报Out of Memory错误
- Out of memory解决办法
- tomcat "out of memory" error 的解决办法
- LR报错:error -26000:error out of memory解决办法
- eclipse out of memory错误的解决方法
- 如何解决tomcat运行报out of memory错误
- CMD下运行Ant报Out of Memory错误 的解决方式
- 【转】Linux 报Out of memory(转) 错误的解决方法
- Matlab “Out of memory”解决办法
- Matlab “Out of memory”解决办法
- 【ORACLE】ORA-27102: out of memory报错的处理
- webpack打包报javaScript heap out of memory的解决方法
- 关于Android out of memory解决办法的总结
- 关于Android out of memory解决办法的总结
- 暂时解决weblogic 的 permGen out of memory错误
- SpringMVC表单标签简介
- Linux spi驱动分析(三)----spiddev分析
- sql 语句(日期所在月的第一天,最后一天)
- DataTable几个常用方法简介
- Android多线程任务优化1:探讨AsyncTask的缺陷
- 调用系统相册报out of memory 错误的解决办法
- phoneGap ios7 适配
- HTTP请求方式 .
- 记录
- Linux spi驱动分析(四)----SPI设备驱动(W25Q32BV)
- Detect browser language——查看浏览器语言
- http://passover.blog.51cto.com/2431658/502232
- linux 几种内建函数的介绍
- 话题讨论:怎样让数据可视化落地?