Skip to content Skip to sidebar Skip to footer

Bitmapfactory Works Perfectly On .png Image But Returns Null When I Load Jpeg Image From Url

BitmapFactory works perfectly on .png image but returns null when I load jpeg image from url. BitmapFactory.decodeStream( (InputStream) new URL(URL).getContent(), null, options);

Solution 1:

try this way may it works it works for me for .jpg image

 Bitmap bitmaps = LoadImage(strMainImageLink, new BitmapFactory.Options()); where **LoadImage** method isas shown below 

LoadImage method

private Bitmap LoadImage(String URL, BitmapFactory.Options options)
  {      
   Bitmapbitmap=null;
   InputStreamin=null;      
      try {
        //  in = OpenHttpConnection(URL);
          bitmap = BitmapFactory.decodeStream(OpenHttpConnection(URL), null, options);
        //  in.close();

      } catch (IOException e1) {
          Log.e("erererere", e1.toString());
      }
      return bitmap;              
  }


private InputStream OpenHttpConnection(String strURL)throws IOException{
 InputStreaminputStream=null;
 URLurl=newURL(strURL);
 URLConnectionconn= url.openConnection();


 try{
  HttpURLConnectionhttpConn= (HttpURLConnection)conn;
  httpConn.setRequestMethod("GET");
  httpConn.connect();

  if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
   inputStream = httpConn.getInputStream();
  }
 }

 catch (Exception ex)
 {
     Log.e("error",ex.toString());
 }
 return inputStream;
}

Solution 2:

Check if your jpg has a color profile set in it. The Android bitmap decoder has failed me because of this. Sadly I did not have the time or resources to find a solution for this.

To know if there is color profile set in the jpg, try to open it with photoshop, if there is it should prompt you to ask you what to do with the profile.

Solution 3:

A URL uniquely identifies a resource on the web. A good way to test a url is with a browser. I have no idea what new URL(URL) is supposed to do. URL takes a string argument but other constructors are available see https://docs.oracle.com/javase/7/docs/api/java/net/URL.html If you pass a string to a constructor you probably want to assign a value to the string or just use a literal string example new URL("google.com") at least you will get something other than null pointer. I have no way of knowing what url your image is because the web is a big place.

Post a Comment for "Bitmapfactory Works Perfectly On .png Image But Returns Null When I Load Jpeg Image From Url"