简体   繁体   中英

Retrieving image from mysql database into android

Good Day Internet!

I am trying to retrieve and display an image from mysql database into an image view (android). The image is a blob type. I have the following php code that gets the image from 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']) . '" />';

    }
?>

Below is my android code for now with the use of JSON.

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

Thanks in advance for any help!

First use Base64 decode the string to byte array:

byte[] data = Base64.decode(imageTemp);
Bitmap b = BitmapFactory.decodeByteArray(data,0,data.length,null);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM