Search code examples
androidbitmapfactory

Error in BitmapFactory.decodeByteArray


Hello

I have the following issue, i'm trying to retrieve the image from openweathermap.com, I have a service that access to the the page and retrieve the image as a byte array, I'm using the same service in an IOS app and work fine so I know that the service is working properly.

Service Code:

public byte[] GetWeatherIcon(string iconDesc)
    {
        byte[] result;
        string url = "http://openweathermap.org/img/w/" + iconDesc + ".png";
        Image image = RequestImage_Get(url, null, null, null);
        System.Drawing.Imaging.ImageFormat format = System.Drawing.Imaging.ImageFormat.Png;
        using (var ms = new MemoryStream())
        {
            image.Save(ms, format);
            result = ms.ToArray();
        }
        return result;
    }

In my Android application I'm executing the following code to access the service:

String urlIcon = SERVICEURL + "/GetWeatherIcon/" + weather.getWeather().get(0).getIcon();
InputStream iconStream = ExecuteRequestService(urlIcon);
BufferedReader rdIcon = new BufferedReader(new InputStreamReader(iconStream));
String jsonIcon = rdIcon.readLine();
JSONObject objIcon = new JSONObject(jsonIcon);
byte[] bytes = objIcon.getString("GetWeatherIconResult").getBytes("UTF-8");
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options);
weather.setImage(bmp);

The method ExecuteRequestService code's is:

protected InputStream ExecuteRequestService(String url) throws Exception{
    try{
        URL urlObj = new URL(url);
        HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection();
        conn.setRequestMethod("GET");
        conn.setDoInput(true);
        if(conn.getResponseCode() == 200) {
            return conn.getInputStream();
        }else {
            return null;
        }
    }catch (Exception ex){
        throw new Exception("An error occurred accessing MiOS server");
    }
}

The error that i'm getting is: D/skia: --- SkImageDecoder::Factory returned null

I was reading a few post with the same exception but all of them are related with BitmapFactory.decodeStream but in my case is different as you can see but i can't find the problem, that's why i need your help

Here is an example of the url that I'm running so you can see the Json:

Url running to get Json

Thanks in advanced


Solution

  • Thank you so much for all your help, after break my neurons y found a good solution for this:

    As you can see the structure of the json I was getting the byte array as String this is the code with the solution:

    String urlIcon = SERVICEURL + "/GetWeatherIcon/" + weather.getWeather().get(0).getIcon();
    InputStream iconStream = ExecuteRequestService(urlIcon);
    BufferedReader rdIcon = new BufferedReader(new InputStreamReader(iconStream));
    String jsonIcon = rdIcon.readLine();
    JSONObject objIcon = new JSONObject(jsonIcon);
    JSONArray jsonArray = objIcon.getJSONArray("GetWeatherIconResult");
    byte[] bytes = new byte[jsonArray.length()];
    for(int i=0; i<jsonArray.length(); i++){
       bytes[i] = (byte) jsonArray.getLong(i);
    }
    Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
    weather.setImage(bmp);
    

    I hope this help a few more like me.