dourong9253 2013-06-20 02:58
浏览 67
已采纳

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

I got confused on the conversion from PHP/JSON, where the imagename(from Mysql) is printed as a long series of characters echo json_encode($response); but in echo '<img src... an image has been displayed. TAG_IMAGE_NAME will contain the Byte Array in String from JSONArray['imageName'] and Include the Image in the Hashmap.


What I wanted is to convert IT(returned from JSONObject 'imagename' using java) to images then store it in the sd card and Populate the image in the listView. Sorry for being confused. Thanks for your consideration.

Table: 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/Java/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条回答 默认 最新

  • douan0729 2013-06-20 03:39
    关注

    The decodedString will be the image name. You can create a string from it if you want like:

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

    You are creating a bitmap image from just the imagename which will not work. This needs to be the actual image NOT the image name.

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

    The php code that encodes it to base64 is commented out, but the java code is still decoding the imageName, is that on purpose.

    It should be noted that json_encode will work fine as long as the field in the database is UTF-8. If the field is UTF-8 then you should need to do any encoding whatsoever and the java parser can just pull out the imagename like the other fields.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图
  • ¥30 乘子法解约束最优化问题的matlab代码文件,最好有matlab代码文件