【问题标题】:Android bitmap image is not saving to MySQL using PHP APIAndroid 位图图像未使用 PHP API 保存到 MySQL
【发布时间】:2023-03-05 17:18:01
【问题描述】:

我想用一些其他字符串值将图像保存到 MySQL 数据库。但是我所有的字符串值都被保存但无法保存图像。我正在将位图图像转换为字符串并将其传递给 PHP 脚本。在我的 PHP 函数中,我试图将其转换为 blob。此代码没有给出任何错误。但是图像没有保存在其他值保存的地方。

以下是我的尝试。

我的 Java 代码:

private void registerUser() {
    final String email = editTextEmail.getText().toString().trim();
    final String password = editTextPassword.getText().toString().trim();
    final String userName = editTextUserName.getText().toString().trim();

    progressDialog.setMessage("Registering User...");
    progressDialog.show();


    //converting image to base64 string
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    userImage.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    byte[] imageBytes = baos.toByteArray();
    final String imageString = Base64.encodeToString(imageBytes, Base64.DEFAULT);


    StringRequest stringRequest = new StringRequest(Request.Method.POST, Constants.URL_REGISTER,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    progressDialog.dismiss();
                    try {
                        JSONObject jsonObject = new JSONObject(response);
                        Toast.makeText(getApplicationContext(), jsonObject.getString("message"),
                                Toast.LENGTH_LONG).show();
                    }
                    catch (JSONException ex) {
                        ex.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    progressDialog.hide();
                    Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
                }
            }){
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<>();
            params.put("username", userName);
            params.put("password", password);
            params.put("email", email);
            params.put("userImage", imageString);
            return params;
        }
    };

    RequestHandler.getInstance(this).addToRequestQueue(stringRequest);

}

我的 PHP 函数:

public function createUser($username, $pass, $email, $userImage) {
        if ($this->isUserExist($username)) {
            return 0;
        } else {
            $password = md5($pass);
            $json_obj = json_decode($userImage);
            $blob = base64_decode($json_obj->blob);
            $stmt = $this->con->prepare("INSERT INTO `users` (`id`, `username`, `password`
                                        , `email`, `user_image`) VALUES (NULL, ?, ?, ?, ?);");
            $stmt->bind_param("sssb", $username, $password, $email, $blob);
            if ($stmt->execute()) {
                return 1;
            } else {
                return 2;
            }
        }
    }

【问题讨论】:

    标签: php android mysql android-volley


    【解决方案1】:

    将您的图像以 byte[] 数组格式发送到您的 api。并将其保存为 mySQL 数据库中的 byte[] 数组。

    【讨论】:

    • 如何用 Volley 的 StringRequest 发送
    • 以这种方式发送,然后在您的 api 中再次将其转换为 byte[] 数组。 params.put("userImage", new String(imageBytes));
    • 不走运,mysql中列的数据类型是wha,我的是blob
    猜你喜欢
    • 2013-08-19
    • 1970-01-01
    • 1970-01-01
    • 2014-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-27
    相关资源
    最近更新 更多