1

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!

1 Answer 1

1

First use Base64 decode the string to byte array:

byte[] data = Base64.decode(imageTemp);
Bitmap b = BitmapFactory.decodeByteArray(data,0,data.length,null);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.