如何将mysql php中的图像显示到android中?

如何将mysql php中的图像显示到android中?,php,android,mysql,image,blob,Php,Android,Mysql,Image,Blob,我目前正在尝试使用图像视图将mysql数据库中的图像显示到我的android程序中。然而,它并没有按照我想要的方式工作。以下是我目前拥有的php代码: <?php error_reporting(E_ALL ^ E_DEPRECATED); require 'connect_aircraftoperator.php'; $image = $db->query("SELECT companyImage FROM company where companyID

我目前正在尝试使用图像视图将mysql数据库中的图像显示到我的android程序中。然而,它并没有按照我想要的方式工作。以下是我目前拥有的php代码:

<?php

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

    $image = $db->query("SELECT companyImage FROM company where companyID = 2");

    $getImage = $image->fetch_assoc();
    $upload = $getImage['companyImage'];
    header("Content-type: image/png");

    echo $upload;
?>

代码在浏览器中显示的图像很好。下面是我当前的android代码

void getImage() {

    //String imageResult = "";
    //JSONObject jArray = null;
    //String Qrimage;
    //Bitmap bmp;

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

        HttpResponse response = httpClient.execute(httpPost);

        HttpEntity entity = response.getEntity();

        is = entity.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
        imageResult = sb.toString();
        is.close();

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

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

    try {

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

        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
        }
        lblTesting3.setText(imageTemp);

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

        imgCompany.setImageBitmap(b);

    }
    catch (Exception e){
        //System.out.println("Exception 3 Caught ");
        Log.e("lag_tag", "Error Parsing Data " + e.toString());
    }
}
void getImage(){
//字符串imageResult=“”;
//JSONObject jArray=null;
//字符串图像;
//位图bmp;
试一试{
//设置默认http客户端
HttpClient HttpClient=新的DefaultHttpClient();
//指定要使用的php文件的url和名称
//作为HttpPost方法的参数
HttpPost HttpPost=新的HttpPost(“http://10.0.2.2//aircraftoperatorapp/leimage.php");
HttpResponse response=httpClient.execute(httpPost);
HttpEntity=response.getEntity();
is=entity.getContent();
}
捕获(例外e){
System.out.println(“捕获异常1”);
}
试一试{
BufferedReader reader=新的BufferedReader(新的InputStreamReader(is,“iso-8859-1”),8;
//创建字符串生成器对象以保存数据
StringBuilder sb=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
//使用toString()方法获取结果中的数据
imageResult=sb.toString();
is.close();
//通过在logcat中打印结果来检查数据
System.out.println(“---这是我的数据---”);
系统输出打印项次(imageResult);
}
捕获(例外e){
System.out.println(“捕获异常2”);
}
试一试{
//创建json数组
JSONArray jArray=新JSONArray(imageResult);
for(int i=0;i
我返回的只是一些可能与我返回的图像有关的文本。以下文字开头是这样的:

ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ


有没有一种方法可以将它转换成一个可以用我现有的代码显示在我的android程序中的图像,或者我必须做一些更不同的事情?我希望任何人都能帮助我!这将意味着很多!提前感谢!

我想你面临的问题是一个简单的解码错误

HttpEntity entity = response.getEntity();

is = entity.getContent();
从HttpEntity获取的InputStream包含二进制图像数据。因此,您只需将该数据复制到bytearray即可:

...
Bitmap bitmap;
byte[] image = null;
...

ByteArrayOutputStream out = new ByteArrayOutputStream();
copy(in, out, true);
image = out.toByteArray();
in.close();

bitmap = BitmapFactory.decodeByteArray(image, 0, image.length);
...

public static void copy(InputStream in, OutputStream out, boolean close)
  throws IOException
{
  if (in != null && out != null)
  {
    byte[] buffer = new byte[4096];
    int count;
    while ((count = in.read(buffer)) > 0) 
      out.write(buffer, 0, count);
    if (close)
    {
      in.close();
      out.close();
    }
  }
}

考虑一下使用类库,你就可以摆脱低级的问题了。谢谢你的建议。我会尽快尝试的。如果我有疑问的话,我会告诉你我的代码应该放在哪里。我在多个地方尝试过,对我来说好像不起作用。我不知道。这取决于你的项目布局。请描述你的文章。我很详细。