将图像从mysql数据库检索到android

时间:2015-04-28 03:26:21

标签: php android mysql imageview blob

美好互联网!

我正在尝试从mysql数据库检索并显示图像到图像视图(android)。图像是blob类型。我有以下PHP代码从mysql获取图像。

<?php

    error_reporting(E_ALL ^ E_DEPRECATED);
    require 'connect_aircraftoperator.php';

    $image = $db->query("SELECT * FROM company");

    while ($row = $image->fetch_assoc()) {
        echo '<img src="data:image/png;base64,' .     base64_encode($row['companyImage']) . '" />';

    }
?>

以下是我使用JSON的Android代码。

    try {
        //setting up the default http client
        HttpClient httpClient = new DefaultHttpClient();

        //specify the url and the name of the php file that we are going to use
        //as a parameter to the HttpPost method
        HttpPost httpPost = new HttpPost("http://10.0.2.2//aircraftoperatorapp/leimage.php");

        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        //getting the response
        HttpResponse response = httpClient.execute(httpPost);

        //setting up the entity
        HttpEntity httpEntity = response.getEntity();

        //setting up the content inside an input stream reader
        //lets define the input stream reader
        is = httpEntity.getContent();

    }
    catch (Exception e) {
        System.out.println("Exception 1 Caught ");
    }

    try {

        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);


        //create a string builder object to hold data
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line+"\n");
        }

        //use the toString() method to get the data in the result
        fullNameResult = sb.toString();
        is.close();

        //checks the data by printing the result in the logcat
        System.out.println("---Here's my data---");
        System.out.println(fullNameResult);

    }
    catch (Exception e){
        System.out.println("Exception 2 Caught ");
    }

    //result now contains the data in the form of json
    //let's inflate it in the form of the list
    try {


        //creates json array
        JSONArray jArray = new JSONArray(fullNameResult);

        for (int i = 0; i < jArray.length(); i++) 
        {
            //create a json object to extract the data
            JSONObject json_data = jArray.getJSONObject(i);
            imageTemp = json_data.getString("companyImage"); //gets the value from the php
        }
//this line should display the image from the mysql database into an image view

    }
        catch (Exception e){
            //System.out.println("Exception 3 Caught ");
            Log.e("lag_tag", "Error Parsing Data " + e.toString());
        }

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

首先使用Base64解码字符串到字节数组:

graph.getClass().isAssignableTo(ctor.getParameterTypes()[0])