我试图从网址加载图片,但它无法正常工作。发生了两个不同的错误。当我放置一个以www开头的URL时,应用程序没有关闭,但既没有加载图像,只显示一个空白区域。但是,当URL以http://开头时,应用程序将关闭。
我用来加载图片的代码是下一个:
public class ThreadAdapter extends BaseAdapter {
ArrayList<Threadx> data;
LayoutInflater inflater;
public ThreadAdapter(Context context, ArrayList<Threadx> data) {
super();
inflater = LayoutInflater.from(context);
this.data = data;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
private static class ViewHolder {
TextView tvSubject;
TextView tvContent;
ImageView imagen;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_row_thread, null);
viewHolder = new ViewHolder();
viewHolder.tvSubject = (TextView) convertView
.findViewById(R.id.tvSubject);
viewHolder.tvContent = (TextView) convertView
.findViewById(R.id.tvContent);
viewHolder.imagen = (ImageView) convertView
.findViewById(R.id.image);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
String subject = data.get(position).getSubject();
String content = data.get(position).getContent();
String imagen = data.get(position).getImagen();
viewHolder.tvSubject.setText(subject);
viewHolder.tvContent.setText(content);
Bitmap bitmap = null;
HttpURLConnection urlConnection = null;
try {
URL url = new URL("www.matrallune.com/images/imagen_corporativa.jpg");
//bitmap = BitmapFactory.decodeStream((InputStream)new URL("www.matrallune.com/images/imagen_corporativa.jpg").getContent());
//bitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream());
urlConnection = (HttpURLConnection) url.openConnection();
InputStream is = urlConnection.getInputStream();
bitmap = BitmapFactory.decodeStream(is);
} catch (IOException e) {
e.printStackTrace();
}
viewHolder.imagen.setImageBitmap(bitmap);
return convertView;
}
}
我尝试了不同的方法,你可以在代码中看到,但所有方法都有相同的问题。我的目的是从变量“imagen”中捕获URL。
答案 0 :(得分:3)
使用此问题&gt;&gt; Loading Bitmap to ImageView from URL in android
public class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
private ImageView imageView;
private Bitmap image;
public DownloadImageTask(ImageView imageView) {
this.imageView = imageView;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
try {
InputStream in = new java.net.URL(urldisplay).openStream();
image = BitmapFactory.decodeStream(in);
} catch (Exception e) {
image = null;
}
return image;
}
@SuppressLint("NewApi")
protected void onPostExecute(Bitmap result) {
if (result != null) {
imageView.setImageBitmap(result);
}
}
}
现在请致电您的代码:
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
conv`enter code here`ertView = inflater.inflate(R.layout.list_row_thread, null);
viewHolder = new ViewHolder();
viewHolder.tvSubject = (TextView) convertView
.findViewById(R.id.tvSubject);
viewHolder.tvContent = (TextView) convertView
.findViewById(R.id.tvContent);
viewHolder.imagen = (ImageView) convertView
.findViewById(R.id.image);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
String subject = data.get(position).getSubject();
String content = data.get(position).getContent();
String imagen = data.get(position).getImagen();
viewHolder.tvSubject.setText(subject);
viewHolder.tvContent.setText(content);
new DownloadImageTask(viewHolder.imagen).execute("www.matrallune.com/images/imagen_corporativa.jpg");
return convertView;
}
答案 1 :(得分:1)
我猜你错过了urlConnection.connect();
将您的代码更改为:
try {
URL url = new URL("www.matrallune.com/images/imagen_corporativa.jpg");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
InputStream is = urlConnection.getInputStream();
bitmap = BitmapFactory.decodeStream(is);
} catch (IOException e) {
e.printStackTrace();
}
不确定。
并且如评论中所建议的那样,使用像Picasso这样的库是明智的。这很容易。只需将代码集成并更改为:
Picasso.with(context).load(url).into(imgView);
更新:尝试包括
connection.setDoInput(true);
setUseCaches(false)
。
答案 2 :(得分:1)
通过Picasso加载图像就像说123,你已经完成了
Picasso
.with(context) //Context
.load("http://i.imgur.com/DvpvklR.png") //Can be Url,File..
.into(imageView)//The ImageView;
这只是正常加载,您可以在Documentation
中找到更多选项根据您的代码
Picasso
.with(context) //Context
.load("http://matrallune.com/images/imagen_corporativa.jpg") //Can be Url,File..
.into(viewHolder.imagen)//The ImageView;
您只想用上面的代码替换图像加载代码。
您的最终适配器
public class ThreadAdapter extends BaseAdapter {
ArrayList<Threadx> data;
LayoutInflater inflater;
private Context mContext;
public ThreadAdapter(Context mContext, ArrayList<Threadx> data) {
super();
inflater = LayoutInflater.from(context);
this.data = data;
this.mContext = mContext;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
private static class ViewHolder {
TextView tvSubject;
TextView tvContent;
ImageView imagen;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_row_thread, null);
viewHolder = new ViewHolder();
viewHolder.tvSubject = (TextView) convertView
.findViewById(R.id.tvSubject);
viewHolder.tvContent = (TextView) convertView
.findViewById(R.id.tvContent);
viewHolder.imagen = (ImageView) convertView
.findViewById(R.id.image);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
String subject = data.get(position).getSubject();
String content = data.get(position).getContent();
String imagen = data.get(position).getImagen();
viewHolder.tvSubject.setText(subject);
viewHolder.tvContent.setText(content);
//Loading image
Picasso
.with(mContext) //Context
.load("http://matrallune.com/images/imagen_corporativa.jpg") //Can be Url,File..
.into(viewHolder.imagen)//The ImageView;
return convertView;
}
}
如果要从Fragment初始化适配器,则应将上下文作为getActivity()传递,或者也可以在片段的onAttach()
中初始化它。
如果要从活动初始化适配器。你只想通过this
。
<强>初始化强>
来自活动
ThreadAdapter tAdapter = new ThreadAdapter(this,data);
来自Fragement的
ThreadAdapter tAdapter = new ThreadAdapter(getActivity(),data);
答案 3 :(得分:0)
使用Picaso Lib http://square.github.io/picasso/致电Picasso.with(context).load("http://imageUrl/image.png").into(imageView);