0

我对从 PHP/JSON 的转换感到困惑,其中图像名(来自 Mysql)被打印为一长串字符 echo json_encode($response);,但在echo '<img src...图像中已显示。TAG_IMAGE_NAME 将包含来自 JSONArray['imageName'] 的字符串中的字节数组,并将图像包含在Hashmap中。


我想要的是将IT(使用java从JSONObject'imagename'返回)转换为图像,然后将其存储在sd卡中并在listView中填充图像。很抱歉让您感到困惑。感谢您的考虑。

表:图像名称

2 | (Binary/Image)  | 32byte    

3 | (Binary/Image)  | 9byte

php/json:

        while ($row = mysql_fetch_array($result)) {
        // temp user array
        $product = array();
        $product["pid"] = $row["groupId"];
        $product["name"] = $row["description"];
        $img = $row["imageName"];
        $b64img = base64_encode ($img);
        $b64img = mysql_real_escape_string($b64img);
        $product["imageName"] = $b64img;
        //echo '<img src="data:image/jpg;base64,' .  base64_encode($img)  . '" />';
        }

……

// echoing JSON response
echo json_encode($response);
{"products":[{"pid":"BEER","name":"sample","imageName":"\/9j\/4AAQSkZJRgABAQEAAAAAAAD...."}]}

安卓/Java/JSONParser:

受保护的字符串doInBackground(字符串... args){

.
.
.
String TAG_IMAGE_NAME = "imageName"; //WILL contain the Byte Array in String

                    for (int i = 0; i < products.length(); i++) {
                    JSONObject c = products.getJSONObject(i);

                    // Storing each json item in variable
                    String id = c.getString(TAG_PID);
                    String name = c.getString(TAG_NAME);
           //Confused with this part
                    byte[] decodedString = Base64.decode(c.getString(TAG_IMAGE_NAME), Base64.DEFAULT);
                    Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);

                   // creating new HashMap
                    HashMap<String, String> map = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    map.put(TAG_PID, id);
                    map.put(TAG_NAME, name);

                    // adding HashList to ArrayList
                    productsList.add(map);
                    }

protected void onPostExecute(String file_url) {
            // dismiss the dialog after getting all products
            pDialog.dismiss();
            // updating UI from Background Thread
            runOnUiThread(new Runnable() {
                public void run() {
                    /**
                     * Updating parsed JSON data into ListView
                     * */
                    ListAdapter adapter = new SimpleAdapter(
                            MainActivity.this, productsList,
                            R.layout.list_item, new String[] { TAG_PID,
                                    TAG_NAME},
                            new int[] { R.id.pid, R.id.name , R.id.list_image});
                    // updating listview
                    setListAdapter(adapter);
                }
            });

        }
4

1 回答 1

1

decodedString 将是图像名称。如果您愿意,可以从中创建一个字符串:

byte[] decodedString = Base64.decode(c.getString(TAG_IMAGE_NAME), Base64.DEFAULT);
String image = new String(decodedString);

您正在从不起作用的图像名创建位图图像。这需要是实际图像而不是图像名称。

Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);

将其编码为base64的php代码已被注释掉,但java代码仍在解码imageName,这是故意的。

需要注意的是,只要数据库中的字段是 UTF-8,json_encode 就可以正常工作。如果该字段是 UTF-8,那么您应该需要进行任何编码,并且 java 解析器可以像其他字段一样提取图像名。

于 2013-06-20T03:39:32.100 回答