php - 从mysql数据库中检索图像到android

标签 php android mysql imageview blob

互联网日安!

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

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

在此先感谢您的帮助!

最佳答案

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

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

关于php - 从mysql数据库中检索图像到android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29909752/

相关文章:

android - 向 parse.com 安装表中的 channel 添加值时出错?

android - RadioGroup onCheckChanged(RadioGroup radioGroup, int checkedId) 无法正常使用动态表单

java - 使用 JPA 针对桥接表设置一对多连接

mysql - 从表中计数,但在某个数字停止计数

Mysql 使用联合结果上的串联字段进一步连接以过滤它们

PHP 语法检查预源代码控制

php - 使用php连接MYSQL并创建数据库(如果不存在)

php - 在使用 curl 时,出于安全原因,如何使我的 cacert.pem 保持最新状态?

php - 使用范围调用模型 laravel 上的未定义关系

android - EditText 没有自动更正等