提兵百万西湖上,立马吴山第一峰!这篇文章主要讲述Android SkImageDecoder :: Factory返回null错误相关的知识,希望能为你提供帮助。
我正在尝试将图像从URL加载到ImageView但发生错误:SkImageDecoder :: Factory返回null。我该如何解决?
这是我的代码:
private class LoadImageFromURL extends AsyncTask<
String, Void, Bitmap>
{
ImageView bitmapImgView;
public LoadImageFromURL(ImageView bmImgView){
bitmapImgView = bmImgView;
}@Override
protected Bitmap doInBackground(String... params) {
// TODO Auto-generated method stub
String urlStr = params[0];
Bitmap img = null;
try {
URL url = new URL(urlStr);
InputStream inputStream = url.openConnection().getInputStream();
//Options bmFactoryOpt = new Options();
//bmFactoryOpt.inJustDecodeBounds = false;
img = BitmapFactory.decodeStream(inputStream);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return img;
}@Override
protected void onPostExecute(Bitmap bitmap){
bitmapImgView.setImageBitmap(bitmap);
}
}
答案【Android SkImageDecoder :: Factory返回null错误】解决了。将代码更改为此。
@Override
protected Bitmap doInBackground(String... params) {
// TODO Auto-generated method stub
String urlStr = params[0];
Bitmap img = null;
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(urlStr);
HttpResponse response;
try {
response = (HttpResponse)client.execute(request);
HttpEntity entity = response.getEntity();
BufferedHttpEntity bufferedEntity = new BufferedHttpEntity(entity);
InputStream inputStream = bufferedEntity.getContent();
img = BitmapFactory.decodeStream(inputStream);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return img;
}
推荐阅读
- Android - 将ImageView设置为URL
- 直接从Android活动下载图像
- 在android下载文件时更新进度条
- Android Firebase下载声音
- Android Studio(如何从自动完成中重新包含已排除的库())
- Chrome for Android显示自动填充即使自动填充(关闭)
- android在java.util.Objects.requireNonNull中获取空指针异常(Objects.java:203)
- 尝试在Android Studio中运行项目时,InstantRunNotificationTask中的NullPointerException
- 将列表添加到Cloud Firebase - Android