Android Studio base64 image to ListView



我正在尝试使用base64图像进行列表视图。列表视图数据是从服务器创建的 json 数组,并创建到哈希映射。

                    JSONObject c = products.getJSONObject(i);
                    // Storing each json item in variable
                    String id = c.getString("pid");
                    String createdAt = c.getString("created_at");
                    String description = c.getString("description");
                    String image = c.getString("image");
                    // creating new HashMap
                    HashMap<String, String> map = new HashMap<String, String>();
                    // adding each child node to HashMap key => value
                    map.put("pid", id);
                    map.put("description", description);
                    map.put("image", image);
                    // adding HashList to ArrayList
                    productsList.add(map);
                }

和:

   protected void onPostExecute(String file_url) {
        // updating UI from Background Thread
        runOnUiThread(new Runnable() {
            public void run() {
                /*Updating parsed JSON data into ListView*/
                ListAdapter adapter = new SimpleAdapter(
                        listActivity.this, productsList,
                        R.layout.list_item, new String[] { "product_id",
                        "product_description", "product_image" },
                        new int[] { R.id.pid, R.id.description, R.id.image });
                // updating listview
                setListAdapter(adapter);
            }
        });
    }

如何使该图像字符串转换为图像视图?我是Android Studio的新手,所以我不知道这是否是明智的方法。

上面的代码来自: http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/如果有帮助的话。

首先,

您要做的是将base64转换为位图,然后可以在imageView上设置它。

如何将base64解码为bitMap:

byte[] decodedString = Base64.decode(encodedImage, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); 

如何将位图设置为图像视图:

ImageView mImg = (ImageView) findViewById(R.id.(your xml img id));
mImg.setImageBitmap(decodedByte );

最新更新