2015-06-14 98 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