【问题标题】:How to display images in simple listview listadapter in android如何在android中的简单listview listadapter中显示图像
【发布时间】:2013-12-13 19:50:05
【问题描述】:

我想在简单的列表视图中显示图像, 我们有 arraylist HashMap,其中包含城市、描述等值。 & 我们必须转换为图像并设置到 ListView 和 List 行 xml 的每一行的 base64 图像字符串是listview_row.xml

这是我在列表视图中显示文本字段的代码,完成:

ArrayList<HashMap<String, String>> arllist = new ArrayList<HashMap<String, String>>();
Intent intent = getIntent();
arllist = (ArrayList<HashMap<String, String>>) intent.getSerializableExtra("map");

int length = arllist.size();
for(int i=0; i<length; i++)
{   
    try{            
        String stringToConvert = arllist.get(i).get("image");
        byte[] decodedString = Base64.decode(stringToConvert, Base64.NO_PADDING);
        Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
        ImageView image = (ImageView)this.findViewById(R.id.img);
        image.setImageBitmap(decodedByte);
    }
    catch (Exception e) {
        // TODO: handle exception
        Toast.makeText(productlist.this, e.toString(), Toast.LENGTH_LONG).show();
    }
}



SimpleAdapter listadapter = new SimpleAdapter(this, arllist, R.layout.list_item,
      new String[] {"city", "category", "description", "mobile", "landmark"},
      new int[] {R.id.city, R.id.category, R.id.desc, R.id.mobile, R.id.landmark});

ListView list=(ListView)findViewById(R.id.list);
list.setAdapter(listadapter);

我的问题是如何将图像转换并绑定到适配器以将其显示在列表视图的每一行 并且在转换图像时也会出错---bad base64

【问题讨论】:

    标签: java android listview android-listview


    【解决方案1】:

    我认为This Tutorial 正是您想要的。

    编辑:

    试试这个:

    Android Encode-Decode

    【讨论】:

    • 但我们没有图像,我们在 ArrayList> arllist 中有 base64 响应...所以首先如何将其转换为位图,因为它给出了“java.lang .IllegalArgumentException:bad base-64" 错误。以及如何将其绑定到简单的适配器.....
    • 我发现我从 php 服务中得到了错误的 base64 字符串,其中 \n 并在末尾添加了另一个文本
    【解决方案2】:

    要转换 Base64 字符串,试试这个-

    假设您的图像数据是一个名为 myImageData 的字符串:

     byte[] imageAsBytes = Base64.decode(myImageData.getBytes());
        ImageView image = (ImageView)this.findViewById(R.id.ImageView);
        image.setImageBitmap(
                BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length)
        );
    

    【讨论】:

    • 嘿,我们使用的代码与您在此处发布的代码相同,但出现错误,因为它给出了“java.lang.IllegalArgumentException: bad base-64”错误。
    • 在我的情况下,它的工作,我猜你的base64字符串不正确。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-02
    • 1970-01-01
    • 2014-05-20
    • 1970-01-01
    • 2019-04-17
    相关资源
    最近更新 更多