网上有很多说是因为没有采用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; }
这样就可以正常下载并显示了,噢耶!!!
时间: 2024-10-05 06:17:53