Android无法将图像保存到mysql数据库

时间:2016-03-12 22:27:00

标签: php android

我不知道是否有人可以阅读所有这些并且无论如何都要花时间 我试图使用Android应用程序将图像保存到mysql数据库,但它失败了,没有添加任何内容,所以我试图显示一些我用来保存图像的变量,以便查看是否有任何错误因此使用Log.d方法我显示两个变量,请查看它们的内容并帮助我找出问题所在:

MainActivity

<br />

String uploadImage

  

9J / 4AAQSkZJRgABAQAAAQABAAD / 2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB ... ECT

字符串结果:此变量显示balise html br:

&#13;
&#13;
public String sendPostRequest(String requestURL,HashMap<String, String> postDataParams) {
    URL url;
    String response = "";
    try {
        url = new URL(requestURL);

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(15000);
        conn.setConnectTimeout(15000);
        conn.setRequestMethod("POST");
        conn.setDoInput(true);
        conn.setDoOutput(true);

        OutputStream os = conn.getOutputStream();
        BufferedWriter writer = new BufferedWriter(
                new OutputStreamWriter(os, "UTF-8"));
        writer.write(getPostDataString(postDataParams));

        writer.flush();
        writer.close();
        os.close();
        int responseCode = conn.getResponseCode();

        if (responseCode == HttpsURLConnection.HTTP_OK) {
            BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            response = br.readLine();
        } else {
            response = "Error Registering";
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return response;
}

private String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException {
    StringBuilder result = new StringBuilder();
    boolean first = true;
    for (Map.Entry<String, String> entry : params.entrySet()) {
        if (first)
            first = false;
        else
            result.append("&");

        result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
        result.append("=");
        result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
    }

    return result.toString();
}
&#13;
&#13;
&#13;

RequestHandler:

<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
		
		$image = $_POST['image'];
		
		require_once('dbConnect.php');
		
		$sql = "INSERT INTO images (image) VALUES (?)";

		$stmt = mysqli_prepare($con,$sql);
		
		mysqli_stmt_bind_param($stmt,"s",$image);
		mysqli_stmt_execute($stmt);
		
		$check = mysqli_stmt_affected_rows($stmt);
		
		if($check == 1){
			echo "Image Uploaded Successfully";
		}else{
			echo "Error Uploading Image";
		}
		mysqli_close($con);
	}else{
		echo "Error";
	}
	?>

问题是一个sendPostRequest Methode,但我找不到有人能找到那里的问题?

这是我使用的脚本php:

&#13;
&#13;
.htaccess
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

1-比较图像的BLOB类型的数据大小和数据大小

  $image = $_POST['image'];

  $image = base64_decode($image );

并且由于您在将图像发送到db之前对其进行了编码,因此您需要在存储之前对其进行解码。