簡體   English   中英

使用json從PHP-MySql服務器到Android獲取圖像

[英]Get images from PHP-MySql server to Android using json

我正在開發一個從php服務器下載圖像並在圖像視圖中顯示圖像的應用程序。但是當我從php頁面接收圖像時

if (!empty($result)) {
    if (mysql_num_rows($result) > 0) {

        $result = mysql_fetch_array($result);

        $user = array();
        $user["image"] = base64_encode($result["image"]);
        $response["success"] = 1;
       $response["image_table"] = array();

        array_push($response["image_table"], $user);
        echo json_encode($response);
    } else {
        $response["success"] = 0;
        $response["message"] = "No Image found";
        echo json_encode($response);
    }

它給了我這樣的json響應

03-30 04:43:44.013: D/Image:(2770): {"success":1,"image_table":   [{"image":"\/9j\/4VeRRXhpZgAASUkqAAgAAAAMAAABBAABAAAA..........
03-30 04:43:44.253: D/skia(2770): --- decoder->decode returned false

我將這個圖像字符串解碼為像這樣的位圖。

json= jsonParser.makeHttpRequest(url_img_address, "GET", params);

        Log.d("Image: ", json.toString());

        try {
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                address = json.getJSONArray(TAG_IMAGE_TABLE);
                for (int i = 0; i < address.length(); i++) {
                    JSONObject c = address.getJSONObject(i);
                    image = c.getString(TAG_IMAGE);
                     byte[] dwimage = Base64.decode(image.getBytes());
                      System.out.println(dwimage);
                      bmp = BitmapFactory.decodeByteArray(dwimage, 0, dwimage.length);
                } 
            } else {

            }
        } catch (JSONException | IOException e) {
            e.printStackTrace();
        }

和即時通訊使用此bm的onotherclass將bmp設置為imageview

   ivProperty.setImageBitmap(bmp);

但是它什么也沒顯示......我的異步任務活動尚未完成,並且仍在繼續運行...我的問題是如何將bmp顯示到imageview以及為什么我的異步任務未完成........提前謝謝...

您需要使用使用base64解碼的二進制解碼,並且您將獲得圖像作為位圖。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM