网络图片下载到手机返回null的问题解决方法

安卓BitmapFactory.decodeStream()返回null的问题解决方法

分类: android   8354人阅读  评论(1)  收藏  举报

今天遇到了一个问题,最终解决,记录下解决方案:

问题:从网络获取图片,数据为InputStream流对象,然后调用BitmapFactory的decodeStream()方法解码获取图片,返回null。

-------------------------------------------------

代码如下:

[java]  view plain copy
  1. private Bitmap getUrlBitmap(String url) {  
  2.     Bitmap bm;  
  3.     try {  
  4.         URL imageUrl = new URL(url);  
  5.         HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection();  
  6.         conn.connect();  
  7.         InputStream is = conn.getInputStream();  
  8.         // byte[] bt=getBytes(is); //注释部分换用另外一种方式解码  
  9.         // bm=BitmapFactory.decodeByteArray(bt,0,bt.length);  
  10.         bm = BitmapFactory.decodeStream(is); // 如果采用这种解码方式在低版本的API上会出现解码问题  
  11.         is.close();  
  12.         conn.disconnect();  
  13.         return bm;  
  14.     } catch (MalformedURLException e) {  
  15.         e.printStackTrace();  
  16.     } catch (IOException e) {  
  17.         e.printStackTrace();  
  18.     }  
  19.     return null;  
  20.   
  21. }  

结果在运行时编译器提示:          DEBUG/skia(xxx):--- decoder->decode returnedfalse 

已经确定从网络获取的数据流没有出现问题,而是在图片解码时出现错误。

经上网查阅资料得知,这个android 的一个bug 。在android 2.2 以下(包括2.2) 用 BitmapFactory.decodeStream() 这个方法,会出现概率性的解析失败的异常。而在高版本中,eg 2.3 则不会出现这种异常。

各种百度、各种谷歌、各种分析问题的过程就不再多说了,这里直接说一个解决方法,如下:

[java]  view plain copy
  1. //定义一个根据图片url获取InputStream的方法  
  2.     public static byte[] getBytes(InputStream is) throws IOException {  
  3.         ByteArrayOutputStream outstream = new ByteArrayOutputStream();  
  4.         byte[] buffer = new byte[1024]; // 用数据装  
  5.         int len = -1;  
  6.         while ((len = is.read(buffer)) != -1) {  
  7.             outstream.write(buffer, 0, len);  
  8.         }  
  9.         outstream.close();  
  10.         // 关闭流一定要记得。  
  11.         return outstream.toByteArray();  
  12.     }  
  13.   
  14. //然后使用方法decodeByteArray()方法解析编码,生成Bitmap对象。  
  15.     byte[] data = getBytesFromInputStream(new URL(imgUrl).openStream());  
  16.     Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length);  
当调用`BitmapFactory.decodeStream(inputStream)`方法返回`null`时,可能是以下原因之一: 1. `inputStream`为空或已关闭,因此需要确保输入流是正确打开并可用的。 2. 图片格式不受支持,例如图片格式为WebP或HEIF,可以使用`Options.inPreferredConfig`设置合适的图片格式。 3. 图片太大,超出了内存限制,可以通过设置`Options.inSampleSize`来减小图片大小,或者使用`BitmapFactory.decodeFile()`方法,将图片保存在本地并逐步加载。 4. 图片压缩方式不受支持,例如图片采用了透明压缩方式,可以使用`Options.inPreferredConfig`设置合适的图片压缩方式。 解决方法: 1. 确保输入流可用并未关闭。 2. 设置合适的图片格式和压缩方式。 3. 减小图片大小或者采用其他方式加载图片。 4. 检查代码是否正确,可以参考以下代码: ``` Bitmap bitmap = null; try { InputStream inputStream = getContentResolver().openInputStream(uri); BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeStream(inputStream, null, options); inputStream.close(); int imageWidth = options.outWidth; int imageHeight = options.outHeight; int scaleFactor = Math.min(imageWidth / targetWidth, imageHeight / targetHeight); inputStream = getContentResolver().openInputStream(uri); options.inJustDecodeBounds = false; options.inSampleSize = scaleFactor; options.inPurgeable = true; options.inInputShareable = true; options.inPreferredConfig = Bitmap.Config.RGB_565; bitmap = BitmapFactory.decodeStream(inputStream, null, options); inputStream.close(); } catch (IOException e) { e.printStackTrace(); } ``` 注意:这里的`uri`是图片的URI,`targetWidth`和`targetHeight`是目标宽度和高度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值