BitmapFactory.decodeByteArray 中的错误
Error in BitmapFactory.decodeByteArray
你好
我有以下问题,我正在尝试从 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 返回空值
我读了一些 post 有同样的例外,但所有这些都与 BitmapFactory.decodeStream 有关,但在我的情况下是不同的,如你所见,但我找不到问题,这就是为什么我需要你的帮助
这是我 运行 的 url 的示例,因此您可以看到 Json:
Url running to get Json
在此先致谢
SkImageDecoder::Factory returned null
?你是说 BitmapFactory.decodeByteArray() returns null
?和往常一样的原因。内存不足。字节数组的大小是多少?图片尺寸是多少?
作为 AsyncTask 中的私有变量 class:
Bitmap bmp = null;
在做后台:
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);
}
非常感谢您的所有帮助,在打破我的神经元之后,您找到了一个很好的解决方案:
如您所见 json 的结构 我将字节数组作为字符串获取 这是带有解决方案的代码:
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);
我希望这对更多像我一样的人有所帮助。
只需使用下面的代码
byte[] img = Base64.decode(Service.GetImage(), Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(img, 0, img.length);
imageid.setImageBitmap(getCircleBitmap(bitmap));
你好
我有以下问题,我正在尝试从 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 返回空值
我读了一些 post 有同样的例外,但所有这些都与 BitmapFactory.decodeStream 有关,但在我的情况下是不同的,如你所见,但我找不到问题,这就是为什么我需要你的帮助
这是我 运行 的 url 的示例,因此您可以看到 Json:
Url running to get Json在此先致谢
SkImageDecoder::Factory returned null
?你是说 BitmapFactory.decodeByteArray() returns null
?和往常一样的原因。内存不足。字节数组的大小是多少?图片尺寸是多少?
作为 AsyncTask 中的私有变量 class:
Bitmap bmp = null;
在做后台:
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);
}
非常感谢您的所有帮助,在打破我的神经元之后,您找到了一个很好的解决方案:
如您所见 json 的结构 我将字节数组作为字符串获取 这是带有解决方案的代码:
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);
我希望这对更多像我一样的人有所帮助。
只需使用下面的代码
byte[] img = Base64.decode(Service.GetImage(), Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(img, 0, img.length);
imageid.setImageBitmap(getCircleBitmap(bitmap));