使用json从PHP-MySql服务器到Android获取图像 [英] Get images from PHP-MySql server to Android using json

查看:69
本文介绍了使用json从PHP-MySql服务器到Android获取图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个从php服务器下载图像并在图像视图中显示图像的应用程序.但是当我从php页面接收图像时

i am devloping an application which download image from php-server and display image in image view ..but when i receive image from php page

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响应

it gives me json response like this

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

我将此图像字符串解码为位图....

i m decoding this image string to bitmap like this....

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的onotherclass

and i m using this bm to onotherclass which set bmp to imageview

   ivProperty.setImageBitmap(bmp);

但是它什么也没显示……我的异步任务活动尚未完成,并且仍在继续运行... 我的问题是如何将bmp显示到imageview以及为什么我的asyncktask尚未完成..... thx ....

but it dosent display anything......my asynck task activity is not finished and it continuesly running... my question is that how to display bmp to imageview and why my asyncktask is not finished........thx in advance....

推荐答案

您需要使用使用base64解码的二进制解码,并且您将获得图像作为位图..

You need to use binary decoding using base64 decode, and you will get image as bitmap..

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

这篇关于使用json从PHP-MySql服务器到Android获取图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆