簡體   English   中英

如何顯示從MySQL PHP到Android的圖像?

[英]How to display an image from mysql php into android?

我目前正在嘗試使用圖像視圖將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());
    }
}

我所返回的只是一些可能與要返回的圖像有關的文本。 下面的文本開頭是這樣的:

ÿØÿá?hExifMM *vž¤¬(1´2Ò‡ièü€'?ü€.....等等。

有沒有一種方法可以將其轉換為可使用我已有的代碼顯示到我的android程序中的圖像,或者我必須做其他更不同的事情? 我希望有人能幫助我! 這將意味着很多! 提前致謝!

我認為您面臨的問題是一個簡單的解碼錯誤。

HttpEntity entity = response.getEntity();

is = entity.getContent();

您從HttpEntity獲得的InputStream包含二進制圖像數據。 因此,您可以簡單地將數據復制到字節數組中:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM