我正在尝试将URL中的图像放在ImageView中,但是我一直收到“ SkImageDecoder :: Factory返回null”错误。
这是我的代码:
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String myUrl= urls[0];
Bitmap myBmp= null;
try {
InputStream in = new URL(urldisplay).openStream();
myBmp= BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return myBmp;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
编辑:当获取第一个图像后应用程序崩溃时的logcat
logcat
public class customAdapter extends ArrayAdapter<String> {
private final Context context;
private final ArrayList<String> values;
static class ViewHolder {
private TextView textViewName;
private TextView textViewType;
private ImageView imageView;
}
public customAdapter(Context context, ArrayList<String> values) {
super(context, R.layout.list_member, values);
this.context = context;
this.values = values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder mViewHolder = null;
if (convertView == null) {
mViewHolder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_member, parent, false);
mViewHolder.textViewName = (TextView) convertView.findViewById(R.id.name);
mViewHolder.textViewType = (TextView) convertView.findViewById(R.id.type);
mViewHolder.imageView = (ImageView) convertView.findViewById(R.id.image);
convertView.setTag(mViewHolder);
}
else {
mViewHolder = (ViewHolder) convertView.getTag();
}
String s = values.get(position);
StringTokenizer st = new StringTokenizer(s, "*");
String name = st.nextToken();
String url_photo = st.nextToken();
String type = st.nextToken();
if (type.equals("CHILD")) {
String count_dev = st.nextToken();
mViewHolder.textViewType.setText(count_dev);
}
else {
mViewHolder.textViewType.setText(type);
}
mViewHolder.textViewName.setText(name);
new DownloadImageTask(mViewHolder.imageView).execute(url_photo);
return convertView;
}
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
// TODO Auto-generated method stub
String urlStr = urls[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) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return img;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
}
根据要求,这是此类的完整代码
最佳答案
尝试在您的后台方法中添加以下代码:
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;
}
关于java - SkImageDecoder::Factory返回null并且URL正确,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38107075/