Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: bitmapfactory.decodestream returns null

Tags:

android

bitmap

I have tried to get the bitmap image from the path of the image. But BitmapFactory.decodeStream returns null value.

Code:

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();

I have searched in more sites, still i did not get the solution.

like image 788
Ponmalar Avatar asked May 18 '12 10:05

Ponmalar


1 Answers

Got a Solution :

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();

The problem was that once you've used an InputStream from a HttpUrlConnection, you can't rewind and use the same InputStream again. Therefore you have to create a new InputStream for the actual sampling of the image. Otherwise we have to abort the HTTP request.

like image 172
Ponmalar Avatar answered Nov 05 '22 00:11

Ponmalar