【问题标题】:image cannot display even url is set into imageview即使将 url 设置为 imageview,图像也无法显示
【发布时间】:2012-05-07 11:00:13
【问题描述】:

这是我的活动。

private ImageLoader testing;
testing = new ImageLoader(this);
imageview = (ImageView)findViewById(R.id.image_alllatestnewstitle);
...
...
...

这是在列表视图中,所以它会显示多张图片。

private void filldata() {
    lv = (ListView) findViewById(android.R.id.list);

    String[] from = new String[] { "particularlatestnewstitle",
            "newscategorytitle", "newsdate" };
    int[] to = new int[] { R.id.text_particularlatestnewstitle,
            R.id.text_newscategorytitle, R.id.text_newsdate };

    fillMaps = new ArrayList<HashMap<String, String>>();
    for (int i = 0; i < webservice.news.size(); i++) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("particularlatestnewstitle", webservice.news.get(i)
                .getNtitle());
        map.put("newscategorytitle", webservice.news.get(i).getNewCatName());
        map.put("newsdate", webservice.news.get(i).getNArticalD());

        fillMaps.add(map);
        imageview = (ImageView)findViewById(R.id.image_alllatestnewstitle);
        imageview.setTag(imagepath[i]);
        testing.DisplayImage(imagepath[i], imageview);
    }
    SimpleAdapter adapter = new SimpleAdapter(this, fillMaps,
            R.layout.main_alllatestnewslist, from, to);
    lv.setAdapter(adapter);

这是我的 ImageLoader 类

public void DisplayImage(String url, ImageView imageView)
{
    imageViews.put(imageView, url);
    Bitmap bitmap=memoryCache.get(url);
    if(bitmap!=null){
        System.out.println(url.toString());
        System.out.println(bitmap.toString());
        imageView.setImageBitmap(bitmap);
    }else
    {
        queuePhoto(url, imageView);
    }
}

这是xml布局

<ImageView
    android:id="@+id/image_alllatestnewstitle"
    android:layout_width="134px"
    android:layout_height="80px"
    android:layout_marginBottom="5px"
    android:layout_marginLeft="10px"
    android:layout_marginRight="10px"
    android:layout_marginTop="5px"
    android:scaleType="centerCrop" />

我正在打印结果

url and bitmap

不为空并显示正确的 url 链接。

但是setimagebitmap的时候没有报错但是imageview也没有显示图片。

有什么问题?

p/s:如果需要,请索取更多代码。

【问题讨论】:

    标签: android bitmap imageview


    【解决方案1】:

    我确定您使用的是 Fedor 的延迟加载逻辑。

    现在,根据您提供的代码,我可以说一件事,即您在调用 DisplayImage() 方法之前忘记将 TAG 设置为 ImageView。

    testing.setTag(imagepath[i]);
    

    【讨论】:

    • ImageLoader 没有这个功能
    • @AlanLai 仅供参考,它的内置函数。
    • 怎么称呼呢?它不应该在测试
    • 我的意思是 imageView.setTag("图片的 URL")
    • 我没有 getview() 方法,我使用的是 simpleadapter 内置函数
    【解决方案2】:

    donwload image 尝试此代码并在imageview 中设置图像。

           try {
                URL url = new URL(urlStr);
                URLConnection ucon = url.openConnection();
    
                InputStream is = ucon.getInputStream();
                Bitmap bitmap = BitmapFactory.decodeStream(is);
                if(bitmap!=null){
                    System.out.println(url.toString());
                    System.out.println(bitmap.toString());
                    imageView.setImageBitmap(bitmap);
                }
            } catch (MalformedURLException e1) {
                e1.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    

    【讨论】:

    • 我从这个变成了现在这个
    • 这是因为这个无法将多张图片加载到listview中,会导致内存不足
    • 在列表视图中使用延迟加载概念可能会解决您的问题。
    • OutOfMemory 发生是因为模拟器中的虚拟内存有限。如果图像尺寸大,则内存不足的问题,所以首先下载图像并将临时保存在 SDCard 中的某个文件夹中,然后使用该图像对该图像进行压缩,使其具有所需的大小和密度。
    猜你喜欢
    • 2011-09-07
    • 1970-01-01
    • 1970-01-01
    • 2014-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-15
    • 1970-01-01
    相关资源
    最近更新 更多