关于BitmapFactory.decodeStream(is)方法无法正常解码为Bitmap对象的解决方法
2012-06-25 15:10
447 查看
关于BitmapFactory.decodeStream(is)方法无法正常解码为Bitmap对象的解决方法
/article/8112017.html[java]
view plaincopyprint?
</pre>在android sdk 1.6版本API帮助文档中,其中关于BitmapFactory.decodeFactory.decodeStream(InputStream is)的帮助文档是这么说明的:<p></p><p></p><pre name="code" class="html">
Bitmap android.graphics.BitmapFactory.decodeStream(InputStream is)
public static Bitmap decodeStream (InputStream is)
Since: API Level 1
Decode an input stream into a bitmap. <strong>If the input stream is null, or cannot be used to decode a bitmap, the function returns null</strong>. The stream's position will be where ever it was after the encoded data was read.
Parameters
is The input stream that holds the raw data to be decoded into a bitmap.
Returns
The decoded bitmap, or null if the image data could not be decoded.
[java] view plaincopyprint? public static Bitmap bmpFromURL(URL imageURL){ Bitmap result = null; try { HttpURLConnection connection = (HttpURLConnection)imageURL .openConnection(); connection.setDoInput(true); connection.connect(); InputStream input = connection.getInputStream(); result = BitmapFactory.decodeStream(input); } catch (IOException e) { e.printStackTrace(); } return result; } public static Bitmap bmpFromURL(URL imageURL){ Bitmap result = null; try { HttpURLConnection connection = (HttpURLConnection)imageURL .openConnection(); connection.setDoInput(true); connection.connect(); InputStream input = connection.getInputStream(); result = BitmapFactory.decodeStream(input); } catch (IOException e) { e.printStackTrace(); } return result; }后来调试发现,result为null,加之查看帮助文档中的黑体字,
所以在所获得的InputStream不为空的情况下,调用BitmapFactory.decodeStream(is)方法,他也有可能无法解码成bitmap,刚开始我怀疑是本身图片地址有问题,或图片自身格式不正确,但通过浏览器查看,图片显示正常,而且,我是保存了几十张图片,但每次都会有个别几张图片无法正常显示,需要重复下载三四次,才可能保存成功。
后来在一篇文章中才发现,原来这是android 1.6版本的一个bug!
有牛人提出的一个解决办法,我试了试,问题解决了
首先在原方法中改一句:
[java]
view plaincopyprint?
result = BitmapFactory.decodeStream(new PatchInputStream(input));
[java] view plaincopyprint? public class PatchInputStream extends FilterInputStream{ protected PatchInputStream(InputStream in) { super(in); // TODO Auto-generated constructor stub } public long skip(long n)throws IOException{ long m=0l; while(m<n){ long _m=in.skip(n-m); if(_m==0l){ break; } m+=_m; } return m; } } public class PatchInputStream extends FilterInputStream{ protected PatchInputStream(InputStream in) { super(in); // TODO Auto-generated constructor stub } public long skip(long n)throws IOException{ long m=0l; while(m<n){ long _m=in.skip(n-m); if(_m==0l){ break; } m+=_m; } return m; } }
第二种方法:最终用的是这种方法
[java]
view plaincopyprint?
InputStream is = httpConn.getInputStream();
[java] view plaincopyprint? if (is == null){ throw new RuntimeException("stream is null"); }else{ try { byte[] data=readStream(is); if(data!=null){ bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); } } catch (Exception e) { e.printStackTrace(); } is.close(); } if (is == null){ throw new RuntimeException("stream is null"); }else{ try { byte[] data=readStream(is); if(data!=null){ bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); } } catch (Exception e) { e.printStackTrace(); } is.close(); }
[java]
view plaincopyprint?
/*
* 得到图片字节流 数组大小
* */
public static byte[] readStream(InputStream inStream) throws Exception{
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while( (len=inStream.read(buffer)) != -1){
outStream.write(buffer, 0, len);
}
outStream.close();
inStream.close();
return outStream.toByteArray();
}
相关文章推荐
- 关于BitmapFactory.decodeStream(is)方法无法正常解码为Bitmap对象的解决方法
- 关于BitmapFactory.decodeStream(is)方法无法正常解码为Bitmap对象的解决方法
- 关于BitmapFactory.decodeStream(is)方法无法正常解码为Bitmap对象的解决方法
- 关于BitmapFactory.decodeStream(is)方法无法正常解码为Bitmap对象的解决方法
- [Android Pro] 关于BitmapFactory.decodeStream(is)方法无法正常解码为Bitmap对象的解决方法
- 教程:关于Google Play无法正常使用解决方法的一些指导建议.
- 关于电脑软件无法正常联网的解决方法
- Android BitmapFactory.decodeStream方法OutOfMemoryError 解决方法
- android中使用BitmapFactory的decodeStream()方法解码图片失败问题
- (应用程序无法正常启动0xc0000142) 关于win10周年版更新后无法正常使用vc6.0问题的解决方法
- BitmapFactory.decodeStream。报错:SkImageDecoder::Factory returned null 原因及解决方法
- 关于wamp server在Windows7 x86&x64上无法正常启动运行的解决方法。
- 关于Visual Studio代码补全(智能提示)无法正常启动的解决方法
- android中使用BitmapFactory的decodeStream()方法解码图片失败问题
- C++6.0(应用程序无法正常启动0xc0000142) 关于win10周年版更新后无法正常使用vc6.0问题的解决方法
- 转载 Android中使用BitmapFactory的decodeStream()方法解码图片失败问题
- (应用程序无法正常启动0xc0000142) 关于win10周年版更新后无法正常使用vc6.0问题的解决方法
- 解决在List中用Bitmap作Image的source时候无法正常显示的方法
- 关于wamp server在Windows7 x86&x64上无法正常启动运行的解决方法。
- Android在加载图片时, BitmapFactory.decodeStream(is)代码报出OOM异常问题解决