Get images from PHP-MySql server to Android using json

China☆狼群 提交于 2019-12-20 05:02:58

问题


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);
    }

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();
        }

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

   ivProperty.setImageBitmap(bmp);

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....


回答1:


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);


来源:https://stackoverflow.com/questions/29342749/get-images-from-php-mysql-server-to-android-using-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!