gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 00:00:56 24 4
gpt4 key购买 nike

互联网日安!

我正在尝试从 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com