解析列表视图中的图像

发布于 2024-12-11 23:30:26 字数 2500 浏览 0 评论 0 原文

我正在解析 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;
    } 

我从谷歌搜索并尝试了不同的方法,但无法解析图像。我犯了什么错误?任何帮助将更加感激。

I am parsing text and images in listview in android app. I have successfully parsed text in listview but failed to parse images. I m also getting url of images such as url/image.jpeg but donot know how to get images in imageview from that url.
Code is:

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);

The method DownloadImage is:

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;
    } 

I have searched from google and tried different ways but can't parse images. What i am making mistake? Any help will be more appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

伴我老 2024-12-18 23:30:27

根据我过去的经验,Android 开发者博客上的这篇文章对我帮助很大,尤其是在多线程和性能问题方面:多线程性能

In my past experiences, this post from Android Developer Blog helped me much, especially with multithreading and performance issues: Multithreading for performance.

纵山崖 2024-12-18 23:30:27

您可能应该使用 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.

寄离 2024-12-18 23:30:26

由于您尚未在代码中实现任何背景图像加载,我建议您执行 在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文