安卓BitmapFactory.decodeStream()返回null的问题解决方法
问题描述:
从网络获取图片,数据为InputStream流对象,然后调用BitmapFactory的decodeStream()方法解码获取图片,返回null。
代码如下:
private Bitmap getUrlBitmap(String url) {
Bitmap bm;
try {
URL imageUrl = new URL(url);
InputStream is = imageUrl .openStream();
bm = BitmapFactory.decodeStream(is); // 如果采用这种解码方式在低版本的API上会出现解码问题
is.close();
return bm;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
解决办法:定义一个根据图片url获取InputStream的方法
public static byte[] getBytes(InputStream is) throws IOException {
ByteArrayOutputStream outstream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024]; // 用数据装
int len = -1;
while ((len = is.read(buffer)) != -1) {
outstream.write(buffer, 0, len);
}
outstream.close(); // 关闭流一定要记得。
return outstream.toByteArray();
}
然后使用方法decodeByteArray()方法解析编码,生成Bitmap对象
byte[] data = getBytesFromInputStream(new URL(imgUrl).openStream());
Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length);
安卓BitmapFactory.decodeStream()返回null的问题解决方法的更多相关文章
- Type.GetType()在跨程序集反射时返回null的解决方法
在开发中,经常会遇到这种情况,在程序集A.dll中需要反射程序集B.dll中的类型.如果使用稍有不慎,就会产生运行时错误.例如使用Type.GetType("BNameSpace.Class ...
- Type.GetType()反射另外项目中的类时返回null的解决方法
项目1:ProjectA namespace ProjectA { public class paa { .... } } Type.GetType("paa")返回null Ty ...
- Linq to Entity 求最大小值Max/Min返回null的处理方法
var maxId=db.user.Select(u=>u.CId).DefaultIfEmpty() 如果maxId返回null则maxId.Max()的值为0
- BitmapFactory.decodeStream(inputStream)返回null的解决方法
场景:Android,通过inputStream从网络上获取图片 随后两次使用BitmapFactory对InputStream进行操作,一次获取宽高,另一次缩放 但是在缩放时,发现inputStre ...
- BitmapFactory.decodeByteArray() 返回null,分析与解决
问题描述:用android自带的Camera获取图片,上传至远程数据库中(mysql),以BLOB格式存储, 但在提取图片时,始终无法在android界面显示,示例代码如下: ..... .... ...
- mysql执行load_fle返回NULL的解决方法
mysql 版本: 5.7.18 问题: 在执行mysql 函数load_file时,该函数将加载指定文件的内容,存储至相应字段.如: SELECT LOAD_FILE("D:\aa.txt ...
- Scrapy爬虫返回302重定向问题解决方法
scrapy爬虫遇到爬取页面时302重定向导致response页面与实际需要爬取的页面信息不一致,导致无法正常获取信息,查看日志存在 scrapy.downloadermiddlewares.redi ...
- BitmapFactory.decodeStream()获取bitmap返回null
正常的图片缩放代码如: ByteArrayOutputStream baos = new ByteArrayOutputStream(); arg1.compress(Bitmap.CompressF ...
- Picasso加载网络图片失败,提示decodestream时返回null
最近遇到一个问题,项目用的图片加载框架是Picasso,网络加载框架是okhttp,项目在加载轮播图时有时可以正常加载,有时,会加载失败,提示decodestream时返回null. 首先,需要确定是 ...
随机推荐
- 系统信号-signal.h
#define SIGSEGV 11 /* segmentation violation */ #define SIGSYS 12 /* bad argument to system call */ ...
- python bs4库
Beautiful Soup parses anything you give it, and does the tree traversal stuff for you. BeautifulSoup ...
- JavaScript--小白入门篇2
一.布尔值和关系运算符.逻辑运算符 1.1 布尔值 我们上篇文章说了,学习了两种变量的类型数值型.字符串型. 实际上,还有很多变量的类型.我们今天再学习一种,叫做“布尔类型”. 数值型里面的值 ...
- CCF201709-1 打酱油 java(100分)
试题编号: 201709-1 试题名称: 打酱油 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 小明带着N元钱去买酱油.酱油10块钱一瓶,商家进行促销,每买3瓶送1瓶,或者每 ...
- Discuz论坛迁移需要修改的3个配置文件
需要修改的3个地方: \config\config_global.php \config\config_ucenter.php \uc_server\data\config.inc.php
- cmd界面中断一个程序快捷键 ctrl+c
cmd界面中断一个程序快捷键 ctrl+c
- BNUOJ 14381 Wavio Sequence
Wavio Sequence Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVA. Origina ...
- [luoguP1901] 发射站(单调栈)
传送门 呵呵 ——代码 #include <cstdio> #include <iostream> #define N 1000010 #define LL long long ...
- noip模拟赛 兔子
[问题描述]在一片草原上有N个兔子窝,每个窝里住着一只兔子,有M条路径连接这些窝.更特殊地是,至多只有一个兔子窝有3条或更多的路径与它相连,其它的兔子窝只有1条或2条路径与其相连.换句话讲,这些兔子窝 ...
- 校长的收藏(洛谷 U4534)
题目背景 XS中学的校长喜欢收集手办,家里面都是价值不菲的手办. 校长喜欢给手办们排队并且对于某些些区间内的手办喜爱有加. 现在,校长外出散步(找乐子),你潜入他的房间打算借(偷走)他的手办炫耀一下. ...