解决android BitmapFactory.decodeStream()返回null
2014-05-04 22:11
477 查看
问题代码: private Bitmap downloadBitmap(String imageUrl) { Bitmap bitmap = null; HttpURLConnection con = null; try { URL url = new URL(imageUrl); con = (HttpURLConnection) url.openConnection(); con.setConnectTimeout(5 * 1000); con.setReadTimeout(10 * 1000); con.setDoInput(true); con.setDoOutput(true); //这里返回的bitmap=null bitmap = BitmapFactory.decodeStream(con.getInputStream()); } catch (Exception e) { e.printStackTrace(); } finally { if (con != null) { con.disconnect(); } } return bitmap; } 解决办法: public 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(); } public byte[] InputStreamTOByte(InputStream in) throws IOException{ ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] data = new byte[1024]; int count = -1; while((count = in.read(data,0,1024)) != -1) outStream.write(data, 0, count); data = null; return outStream.toByteArray(); } byte[] data = InputStreamTOByte(new URL(imgUrl).openStream()); Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length);
相关文章推荐
- 安卓BitmapFactory.decodeStream()返回null的问题解决方法
- Android异步加载图片资源,BitmapFactory.decodeStream返回null的问题
- BitmapFactory.decodeStream(inputStream)返回null的解决方法
- 安卓BitmapFactory.decodeStream()返回null的问题解决方法
- Android异步从网络加载图片BitmapFactory.decodeStream 返回null的问题
- android获取网络图片的用法 BitmapFactory.decodeByteArray 返回null
- android BitmapFactory.decodeFile()返回为null问题
- Android BitmapFactory.decodeFile(filePath, options) 返回 Null 6.0权限
- BitmapFactory.decodeStream。报错:SkImageDecoder::Factory returned null 原因及解决方法
- BitmapFactory.decodeStream方法返回null的错误分析
- Android在加载图片时, BitmapFactory.decodeStream(is)代码报出OOM异常问题解决
- BitmapFactory.decodeStream方法返回null的错误分析
- BitmapFactory.decodeByteArray() 返回null,分析与解决
- BitmapFactory.decodeByteArray() 返回null,分析与解决
- BitmapFactory.decodeStream返回null
- Android BitmapFactory.decodeStream方法OutOfMemoryError 解决方法
- BitmapFactory.decodeStream(inputStream,null,options)返回为null
- Android 压缩网络上的图片BitmapFactory.decodeStream()返回为空的问题
- BitmapFactory.decodeStream()获取bitmap返回null
- [Android Pro] 关于BitmapFactory.decodeStream(is)方法无法正常解码为Bitmap对象的解决方法