使用 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 用于另一个将 bmp 设置为 imageview 的类

   ivProperty.setImageBitmap(bmp);

但它没有显示任何内容......我的异步任务 activity 尚未完成,它继续 运行... 我的问题是如何将 bmp 显示到 imageview 以及为什么我的 asyncktask 没有完成......提前谢谢....

您需要使用base64解码进行二进制解码,您将得到位图形式的图像..

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