android 下载图片出现SkImageDecoder::Factory returned null,BitmapFactory.Options压缩
2024-10-12 11:39:37
网上有很多说是因为没有采用HttpClient造成的,尼玛,我改成了HttpClient 请求图片之后还是会出现SkImageDecoder::Factory returned null,
但是直接使用
bitmap = BitmapFactory.decodeStream(is); 是正常的,但解决不了图片大内存溢出的问题
解决办法:
重新获取一次流,注意看代码(红色部分):
/**
* 根据网络url获取bitmap对象
* @param url
* @param width 要获取的宽度
* @param height 要获取的高度 防止内存溢出
* @return
*/
public static Bitmap returnBitMap(String url, int width,int height) { Bitmap bitmap = null;
try {
HttpGet httpRequest = new HttpGet(url);
Log.d("returnbitmap", "url="+url);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
HttpEntity entity = response.getEntity();
BufferedHttpEntity bufferedHttpEntity = new BufferedHttpEntity(entity);
InputStream is = bufferedHttpEntity.getContent();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true; //只获取图片的高宽
int scale = 1;
BitmapFactory.decodeStream(is,null,options);
int w = options.outWidth;
int h = options.outHeight;
Log.d("returnbitmap", "w="+w+";h="+h+";width="+width+";height="+height);
while(true)
{
if ((width>0 && w < width) || (height>0 && h < height))
{
break;
}
w /= 2;
h /= 2;
scale *= 2;
}
options.inJustDecodeBounds = false;
options.inSampleSize = scale;
is = bufferedHttpEntity.getContent();//重新获取流
bitmap = BitmapFactory.decodeStream(is,null,options);
Log.d("returnbitmap", "bitmap="+bitmap+(bitmap==null?"bitmap is null":"bitmap is not null"));
is.close();
} catch (IOException e) {
e.printStackTrace();
}
catch (OutOfMemoryError e) {
e.printStackTrace();
}
return bitmap;
}
这样就可以正常下载并显示了,噢耶!!!
最新文章
- 大叔最新课程~MVC核心技术剖析
- NoSQL数据库笔谈(转)
- bzoj1188 [HNOI2007]分裂游戏 博弈论 sg函数的应用
- [转载] 使用MySQL Proxy解决MySQL主从同步延迟
- iOS - Xcode 插件
- ADO.NET 基础
- javax.xml.ws.webserviceexception class do not have a property of the name
- xml学习总结(四)
- Delphi重载,覆盖,多态
- 数据结构(主席树):HDU 4729 An Easy Problem for Elfness
- Linux显示系统的诊断信息
- Fluent Interface(流式接口)
- windows下 jdk1.7安装教程图解
- ASP.NET上传大文件报错,IIS7.0
- 【ARTS】01_12_左耳听风-20190128~20190203
- I2S接口介绍
- 【转】AlphaGo与人工智能
- Shiro系列(2) - 权限模型以及权限分配的两种方式
- The Go scheduler
- c++11 List 容器