android位图图像没有使用php api保存到mysql

njthzxwz  于 2021-06-24  发布在  Mysql
关注(0)|答案(1)|浏览(223)

我想用其他字符串值将一个图像保存到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);

}

im我的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;
            }
        }
    }
bvuwiixz

bvuwiixz1#

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

相关问题