2015-06-14 95 views
1

我正在尝试使用base64图像做listview。 Listviews数据是来自服务器的json-array,并创建为hashmap。Android Studio base64图像到ListView

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

    } 

我怎样才能让转换为imageview的这种形象字符串? 我是新的Android Studio,所以我不知道这是否聪明的方式做到这一点..

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

回答

0

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

的base64如何解码为位图:

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

如何位图中设置到ImageView的:

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

如何与ListView的工作?这对一个图像很好,但我无法创建ListViews图像。 – Vmaatta