BitmapFactory.decodeByteArray

时间:2015-11-02 19:42:27

标签: android bitmapfactory

您好

我有以下问题,我正在尝试从openweathermap.com检索图像,我有一个访问该页面的服务并将该图像作为字节数组检索,我正在使用相同的服务IOS应用程序和工作正常,所以我知道该服务正常工作。

服务代码:

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;
    }

在我的Android应用程序中,我正在执行以下代码来访问该服务:

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

ExecuteRequestService代码的方法是:

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");
    }
}

我得到的错误是:D / skia:--- SkImageDecoder :: Factory返回null

我正在阅读一些有相同异常的帖子,但所有这些都与BitmapFactory.decodeStream有关,但在我的情况下是不同的,因为你可以看到,但我找不到问题,这就是为什么我需要你的帮助

以下是我正在运行的网址示例,您可以看到Json:

Url running to get Json

先谢谢

4 个答案:

答案 0 :(得分:0)

SkImageDecoder::Factory returned null?你的意思是BitmapFactory.decodeByteArray() returns null?同样的原因。内存不足。字节数组的大小是多少?图片的大小是多少?

答案 1 :(得分:0)

作为AsyncTask类中的私有变量:

Bitmap bmp = null;

在doinbackground中:

JSONObject objIcon = new JSONObject(jsonIcon);

String thevalues = objIcon.get("GetWeatherIconResult").toString();

thevalues = thevalues.replace("[", "");
thevalues = thevalues.replace("]", "");

result = thevalues;

String values[] = thevalues.split(",");

byte bytes[] = new byte [values.length];

int nr = -1;    
while (++nr < values.length)
    {
    int value = Integer.valueOf(values[nr]);
    bytes[nr] = (byte)value;    
    }

bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);

在onPostExecute:

if ( bmp != null )
        {
            ImageView imageView = (ImageView)MainActivity.this.findViewById ( R.id.imageView);

            imageView.setImageBitmap(bmp);
        }

答案 2 :(得分:0)

非常感谢你的帮助,在休息后我的神经元为此找到了一个很好的解决方案:

正如你可以看到json的结构我将字节数组作为字符串这是解决方案的代码:

ArrayList<Object> al = new ArrayList<>();
al.add("mmm");
al.add(1);
al.add("ka");

for(Object a : al){
  if(a instanceof Integer){
   System.out.print(a); 
  }
}

我希望这有点像我一样帮助。

答案 3 :(得分:0)

只需使用以下代码

即可
byte[] img = Base64.decode(Service.GetImage(), Base64.DEFAULT);
                    Bitmap bitmap = BitmapFactory.decodeByteArray(img, 0, img.length);
                    imageid.setImageBitmap(getCircleBitmap(bitmap));