我试图从图像的路径获取位图图像.但BitmapFactory.decodeStream返回null值.
码:
URL url = new URL(path); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoInput(true); connection.connect(); InputStream input = connection.getInputStream(); Bitmap myBitmap = BitmapFactory.decodeStream(input); connection.disconnect(); input.close();
解决方法
得到了解决方案:-)
HttpGet httpRequest = new HttpGet(URI.create(path) ); HttpClient httpclient = new DefaultHttpClient(); HttpResponse response = (HttpResponse) httpclient.execute(httpRequest); HttpEntity entity = response.getEntity(); BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity); bmp = BitmapFactory.decodeStream(bufHttpEntity.getContent()); httpRequest.abort();
问题是,一旦你使用了来自HttpUrlConnection的InputStream,你就不能再回放并再次使用相同的InputStream.因此,您必须为图像的实际采样创建新的InputStream.否则我们必须中止http请求.