我试图在android上显示来自mysql数据库的图像。我将我的数据库连接到mysql,它正在检索Web上的数据,但是当我在android it shows no output
上发送数据时。
这是php
文件:
$con = new mysqli ($server_name, $mysql_user, $mysql_pass, $db_name);
if (mysqli_connect_errno())
{ echo "Failed to connect to MySQL: " . mysqli_connect_error();}
if (isset($_POST['id'])) {}
$id=(int)$_POST['id'];
$sql = "SELECT c_image FROM images WHERE criminal_criminal_id=$id";
$r_query = $con->query($sql);
$i=0;
while($row = $r_query->fetch_assoc()) {
$response[$i]= array( "Match_id"=> $row['image_id'],"image"=>$row["c_image"]);
$i++;
$image=$row['c_image'];
echo '<img src="data:image/jpeg;base64,'.base64_encode($row['c_image']).'"/>'; }
echo json_encode(array("user_data"=>$response));
MainActivity.java
public class MainActivity extends Activity {
ImageView imageview;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
BackGround b = new BackGround();
b.execute();
}
class BackGround extends AsyncTask<String ,String, String> {
@Override
protected String doInBackground(String... params) {
String data="";
int tmp;
try {
URL url = new URL("http://192.168.15.60/fetchimage.php");
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setDoOutput(true);
InputStream is = httpURLConnection.getInputStream();
while((tmp=is.read())!=-1){
data+= (char)tmp;
}
is.close();
httpURLConnection.disconnect();
return data;
} catch (MalformedURLException e) {
e.printStackTrace();
return "Exception 1: "+e.getMessage();
} catch (IOException e) {
e.printStackTrace();
return "Exception 2: "+e.getMessage();
}
}
protected void onPostExecute(String data) {
String image="";
try {
JSONObject json = new JSONObject(data.toString());
JSONArray jArray = json.getJSONArray("user_data");
for (int i = 0; i <= jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
image= json_data.getString("image");
}
byte[] rawImage = Base64.decode(image, Base64.DEFAULT);
Bitmap bmp = BitmapFactory.decodeByteArray(rawImage, 0, rawImage.length);
imageview= (ImageView) findViewById(R.id.imageView);
imageview.setImageBitmap(bmp);
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
}
}
}
activity_main.xml中
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginStart="48dp"
android:layout_marginTop="36dp"
/>
</RelativeLayout>