解析列表视图中的图像
我正在解析 Android 应用程序中列表视图中的文本和图像。我已成功解析列表视图中的文本,但无法解析图像。我还获取图像的 url,例如 url/image.jpeg,但不知道如何从该 url 获取 imageview 中的图像。 代码是:
NodeList nodes = doc.getElementsByTagName("item");
for (int i = 0; i < nodes.getLength(); i++)
{
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element)nodes.item(i);
bitmap = DownloadImage(XMLfunctions.getValue(e, "thumb"));
image = (ImageView) findViewById(R.id.image);
image.setImageBitmap(bitmap);
Log.e("imageView","image is:"+image);
//map.put("id", XMLfunctions.getValue(e, "id"));
map.put("title", XMLfunctions.getValue(e, "title"));
map.put("author", XMLfunctions.getValue(e, "author"));
map.put("catagory",XMLfunctions.getValue(e, "catagory"));
mylist.add(map);
}
ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.mainview,
new String[] {"title","author", "catagory" },
new int[] {R.id.title, R.id.author,R.id.catagory });
setListAdapter(adapter);
DownloadImage 方法是:
private Bitmap DownloadImage(String URL)
{
Log.v("url",URL);
Bitmap bitmap = null;
InputStream in = null;
try {
in = openHttpConnection(URL);
bitmap = BitmapFactory.decodeStream(in);
Log.e("image","image"+bitmap);
in.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return bitmap;
}
private InputStream openHttpConnection(String urlStr) {
InputStream in = null;
int resCode = -1;
try {
URL url = new URL(urlStr);
URLConnection urlConn = url.openConnection();
if (!(urlConn instanceof HttpURLConnection)) {
throw new IOException ("URL is not an Http URL");
}
HttpURLConnection httpConn = (HttpURLConnection)urlConn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
resCode = httpConn.getResponseCode();
if (resCode == HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return in;
}
我从谷歌搜索并尝试了不同的方法,但无法解析图像。我犯了什么错误?任何帮助将更加感激。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据我过去的经验,Android 开发者博客上的这篇文章对我帮助很大,尤其是在多线程和性能问题方面:多线程性能。
In my past experiences, this post from Android Developer Blog helped me much, especially with multithreading and performance issues: Multithreading for performance.
您可能应该使用 BitmapFactory.decodeStream(inputstream); ,
因此这会返回一个位图,以便现在您可以在图像视图上设置它。
希望对你有帮助。
You should probably be using
BitmapFactory.decodeStream(inputstream);
so this returns you a bitmap so that now you can set it on an imageview.
hope it helps you.
由于您尚未在代码中实现任何背景图像加载,我建议您执行 在 ListView 中延迟加载图像 我认为这对您的情况最有用。如果您没有实现任何背景图像获取线程过程,那么当获取整个数据时,屏幕将挂起并再次释放到实际模式。
As you haven't implemented any background image loading in your code, i suggest you to go through Lazy Loading images in ListView which i think mostly useful in your case. If you don't implement any background image fetching threading process then the screen will get hang and released again to actual mode when whole data is fetched.