我有一个包含十个图像 url 源的 String[],我正在尝试让 ImageView 显示它们。我必须显示每个图像及其旁边的相应文本,即使文本显示图像也不起作用。我一直在尝试我能找到的所有解决方案,但似乎没有任何效果。我将不胜感激一些指导。
这是我正在尝试的解决方案:
我从扩展 ListActivity 的类中调用的方法 (titles 和 imageurls 都是 String[],imageurls 包含图片 url 源,titles 包含要显示在图片旁边的文本)
setListAdapter(new MyPerformanceArrayAdapter(this, titles, imageurls));
这是我的 MyPerformanceArrayAdapter 代码:
public class MyPerformanceArrayAdapter extends ArrayAdapter<String> {
private final Activity context;
private final String[] names;
private final String[] images;
public MyPerformanceArrayAdapter(Activity context, String[] names, String[] images) {
super(context, R.layout.rowlayout, names);
this.context = context;
this.names = names;
this.images = images;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.rowlayout, null, true);
TextView textView = (TextView) rowView.findViewById(R.id.label);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
String s = names[position];
textView.setText(s);
Drawable drawable = LoadImageFromWebOperations(images[position]);
imageView.setImageDrawable(drawable);
return rowView;
}
private Drawable LoadImageFromWebOperations(String url) {
try {
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
} catch (Exception e) {
System.out.println("Exc="+e);
return null;
}
}
}
非常感谢!
最佳答案
如果您使用来自 sdcard 的路径,下面的代码将起作用,否则如果您使用来自 drwable-res 文件夹的图像,请像为字符串数组一样创建一个静态位图数组
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.rowlayout, null, true);
TextView textView = (TextView) rowView.findViewById(R.id.label);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
String s = names[position];
textView.setText(s);
//此处完成的更改
Bitmap bmp = LoadImageFromWebOperations(images[position]);
imageView.setImageBitmap(bmp);
return rowView;
}
//
private Drawable LoadImageFromWebOperations(String url) {
try {
// use bitmap instead of drawable
Bitmap bmp=BitmapFactory.decodeStream(getContentResolver().openInputStream(capturedImage));
return bmp;
} catch (Exception e) {
System.out.println("Exc="+e);
return null;
}
}
关于android - 来自 URL 的图像未显示在 ImageView 中,我已经尝试了所有我能找到的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8626557/