开发者社区> 问答> 正文

android imagview 加载大图片的问题?报错

@ViewInject(R.id.img_thumbnail)
 private ImageView pic;

Uri uri = Uri.parse("file://" + uploadFilePath);
     pic.setVisibility(View.VISIBLE);
     pic.setImageURI(uri);

我加载的一个图片大于7m,小于11m后,pic显示图片区域是白板一片,如果大于11m后,程序直接报错

java.lang.OutOfMemoryError:

请求解决办法?

 

展开
收起
爱吃鱼的程序员 2020-06-14 18:49:54 609 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    图片肯定要处理过再显示 不能直接显示原图根据当前ImageView的宽高,还有图片需要显示的区域,动态裁剪和压缩图片生成bitmap,再填充ImageView回复<aclass='referer'target='_blank'>@iuKa:这样的话,总的像素还是那么多啊?而且应该不会被回收掉的吧?回复<aclass='referer'target='_blank'>@黑狗:感觉很多时候技术实现方式还是要同业务场景结合起来确定<atarget='_blank'>@黑狗是的,在循环里面压缩通常效果都不是很好,稍微控制不好就容易模糊。后来又想了一个办法,解决超大图片加载的问题。就是像地图一样,把大图片切成很多个小图片,在surfaceview里面动态绘制,回复<aclass='referer'target='_blank'>@iuKa:按照通常的那种压缩方法写个for循环来弄,图片非常模糊。。。裁减倒是一种方案,但是要看具体的需求回复<aclass='referer'target='_blank'>@iuKa:哦哦要裁减和压缩当然。。。但是不建议这样我之前也是裁减压缩但是一般就不满足体验要求了

    废话不说,直接上代码

                BitmapFactory.Optionso=newBitmapFactory.Options();
          o.inJustDecodeBounds=true;
          fis=newFileInputStream(path);
          BitmapFactory.decodeStream(fis,null,o);
          fis.close();
          intwidthIn=o.outWidth,heightIn=o.outHeight;
          intscale=1;
          while(true){
            if(widthIn/2<width||heightIn/2<height)break;
            widthIn /=2;
            heightIn/=2;
            scale*=2;
          }
          BitmapFactory.Optionsop=newBitmapFactory.Options();
          op.inSampleSize=scale;
          fis=newFileInputStream(path);
          bitmap=BitmapFactory.decodeStream(fis,null,op);
          fis.close();

    http://stackoverflow.com/questions/14235287/suggestions-to-avoid-bitmap-out-of-memory-error/25868390#25868390

    仔细看,包括大家的回复

    有很多种解决的方案

    图片的bitmap是非常非常大的,根据像素,位,不是11m的图片实际占11m的内存

    这里有个中文的:

    http://fly-ever.iteye.com/blog/1941986

    <atarget="_blank"rel="nofollow">http://www.oschina.net/p/xutils

    直接用别人的xutils

    2020-06-14 18:50:13
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载