Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting input stream into bitmap

I have problems converting a input stream from web into bitmap. Problem occurs only when input image type is .BMP (bitmap). In that case: bitmapFactory.decodeStream returns null.

Any hints how to fix this problem or where should I continue my debugging?

Platform: Android (Honeycomb)

URLConnection conn = url.openConnection();
conn.connect();

inputStream = conn.getInputStream();

bufferedInputStream = new BufferedInputStream(inputStream);

bmp = BitmapFactory.decodeStream(bufferedInputStream);
like image 701
Indrek Kõue Avatar asked Jul 07 '11 14:07

Indrek Kõue


2 Answers

Thank you @Amir for point out the log. Discovered a line:

decoder->decode returned false

This seems to be a common problem. Doing a search I found a solution.

My previous code:

URLConnection conn = url.openConnection();
conn.connect();

inputStream = conn.getInputStream();

bufferedInputStream = new BufferedInputStream(inputStream);

bmp = BitmapFactory.decodeStream(bufferedInputStream);

Code which is working:

HttpGet httpRequest = null;

try {
    httpRequest = new HttpGet(url.toURI());
} catch (URISyntaxException e) {
    e.printStackTrace();
}

HttpClient httpclient = new DefaultHttpClient();

HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);

HttpEntity entity = response.getEntity();

BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);

InputStream instream = bufHttpEntity.getContent();

bmp = BitmapFactory.decodeStream(instream);

Source

like image 103
Indrek Kõue Avatar answered Oct 04 '22 09:10

Indrek Kõue


Here is a one-line answer

val bitmap = BitmapFactory.decodeStream(inputStream)

Returns a Bitmap

like image 36
Favour Felix Chinemerem Avatar answered Oct 04 '22 09:10

Favour Felix Chinemerem