1

我正在开发我的 android 应用程序。我一直在研究和尝试所有不同的方法来从我的服务器 url 中检索图像并将其显示给我的listviewusing SimpleAdapter.

下载图像代码

public Bitmap download(final String image) 
{
    URL myFileUrl = null;
    try {
        myFileUrl = new URL("http://SERVER URL/images/"+image);
        HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
        conn.setDoInput(true);
        conn.connect();
        //int length = conn.getContentLength();
        InputStream is = conn.getInputStream();
        bmImg = BitmapFactory.decodeStream(is);
    } catch (MalformedURLException e) {
        // imageLoadedHandler.sendEmptyMessage(FAILED);
    } catch (IOException e) {
        // imageLoadedHandler.sendEmptyMessage(FAILED);
    }
    return bmImg;
}

将下载的图像放入 HASHMAP

// IMAGE HASHMAP
HashMap<String, Object> map = new HashMap<String, Object>();
map.put(TAG_PHOTO, download(c.getString(TAG_PHOTO)));
applicantsList.add(map);

将解析的 JSON 数据更新到 ListView

adapter = new SimpleAdapter(
                            SignUpApplicantActivity.this, applicantsList,
                            R.layout.list_applicant, new String[] {
                                    TAG_UID, TAG_NAME, TAG_OVERALL,
                                    TAG_APPLY_DATETIME, TAG_PHOTO},
                            new int[] { R.id.applicantUid,
                                    R.id.applicantName,
                                    R.id.applicantOverall,
                                    R.id.apply_datetime, R.id.list_image});

                    adapter.setViewBinder(new SimpleAdapter.ViewBinder() {
                        @Override
                        public boolean setViewValue(View view, Object data,String textRepresentation) 
                        {
                            if((view instanceof ImageView) & (data instanceof Bitmap)) 
                            {
                                ImageView iv = (ImageView) view;
                                Bitmap bm = (Bitmap) data;
                                iv.setImageBitmap(bm);
                                return true;
                            }
                            return false;
                        }
                    });

                    // updating listView
                    setListAdapter(adapter);

在尝试了上述方法后(我认为这是我研究中最接近的答案),图像仍然没有显示在我的应用程序中。我需要一些帮助!提前致谢!

4

0 回答 0