Android: bitmapfactory.decodestream returns null

I tried to get a bitmap from the image path. But BitmapFactory.decodeStream returns null .

the 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 searched in other sites, but I did not get a solution.

+6
source share
5 answers

Received 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 use an InputStream from an HttpUrlConnection, you cannot rewind and use the same InputStream again. Therefore, you need to create a new InputStream to actually fetch the image. Otherwise, we must abort the http request.

+14
source
 public Bitmap getBitmapFromUrl(String url) { Bitmap bm = null; InputStream is = null; BufferedInputStream bis = null; try { URLConnection conn = new URL(url).openConnection(); conn.connect(); is = conn.getInputStream(); bis = new BufferedInputStream(is, 8192); bm = BitmapFactory.decodeStream(bis); } catch (Exception e) { e.printStackTrace(); } finally { if (bis != null) { try { bis.close(); } catch (IOException e) { e.printStackTrace(); } } if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } return bm; } 

Remember to call this in a thread (not the main thread)

+1
source

using the following iam code able to download image from url

 String IMAGE_URL = "http://www.kolkatabirds.com/rainquail8vt.jpg"; //where we want to download it from URL url; try { url = new URL(IMAGE_URL); //open the connection URLConnection ucon = url.openConnection(); //buffer the download InputStream is = ucon.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is,1024); //get the bytes one by one int current = 0; while ((current = bis.read()) != -1) { baf.append((byte) current); } //convert it back to an image ByteArrayInputStream imageStream = new ByteArrayInputStream(baf.toByteArray()); Bitmap theImage = BitmapFactory.decodeStream(imageStream); img.setImageBitmap(theImage); 
0
source

BufferedInputStream is required before the decoder ....

Try it, it works great for me,

BufferedInputStream buf = new BufferedInputStream(inputsteam, 1024);

Passing buf to decode the stream will work fine.

Bitmap theImage = BitmapFactory.decodeStream(buf);

Finally, set the bitmap.

0
source

I had the same problem, but in my case the problem was in the resource (image). Make sure the image is not in CMYK color mode, since Android does not support CMYK images. See this question for more details.

Good luck;)

0
source

Source: https://habr.com/ru/post/916082/


All Articles