使用从Mysql到Android的base64更好地理解

时间:2013-06-20 02:58:34

标签: java php android json bytearray

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


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

表:imagename

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...."}]}

的Android /爪哇/ JSONParser:

protected String doInBackground(String ... 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);
                }
            });

        }

1 个答案:

答案 0 :(得分:1)

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

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解析器可以像其他字段一样提取图像名称。