将图像插入mysql时出错

时间:2016-09-16 06:30:51

标签: php android mysql base64 blob

我正在构建一个Android应用程序,我在其中添加拍照或从库中选择它的可能性,然后将其发送到远程php / mysql服务器。 当我拍照时,它会成功存储并快速存入服务器,但是当我从图库中选择它时,发送错误需要一些时间

 ErrorException: Error while sending STMT_CLOSE packet. PID=99835 in "Path to my php file"

我认为这个错误是由于处理这两种情况的不同(拍照或选择现有照片)。

这是我的java代码的样子:

  private Bitmap thumbnail = null;
         ......
             if (options[item].equals("Take Photo"))
            {
                Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent, 1);
            }
            else if (options[item].equals("Choose from Gallery"))
            {
                Intent intent = new   Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(intent, 2);

            }


         ......
        // onActivityResult
        if (requestCode == 1) {
            thumbnail = (Bitmap) data.getExtras().get("data");
            //photo_profile.setImageBitmap(thumbnail);
        } else if (requestCode == 2) {

            Uri selectedImage = data.getData();
            String[] filePath = { MediaStore.Images.Media.DATA };
            Cursor c = getContentResolver().query(selectedImage,filePath, null, null, null);
            c.moveToFirst();
            int columnIndex = c.getColumnIndex(filePath[0]);
            String picturePath = c.getString(columnIndex);
            c.close();
            thumbnail = (BitmapFactory.decodeFile(picturePath));
        }

然后我创建一个Byte Array来发送到远程服务器。

           byte[] photo = null;
            if (thumbnail != null) {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                thumbnail.compress(Bitmap.CompressFormat.PNG, 100, baos);
                photo = baos.toByteArray();
            }

然后我将图像编码为字符串

        String image_str = Base64.encodeToString(photo, Base64.DEFAULT);
        params.put("photo", image_str); // add it to the request parameters

当我尝试在我的PHP代码中打印照片参数时,我可以看到当使用相机拍摄照片时,它比同一个变量太长,所以我怀疑我的编码出了问题。

我的SQL表中的照片是LONGBLOB类型,这是启动错误的php代码部分:

        $stmt = $this->conn->prepare("UPDATE users set name = ?, email = ?, mobile = ?, photo = ? WHERE id = ?");

        $stmt->bind_param("ssssi", $name, $email, $mobile, $photo, $user_id);
        $stmt->execute();
        $num_affected_rows = $stmt->affected_rows;
        $stmt->close(); // The above error is triggered here!

有没有人知道为什么会发生这种错误?从图库中选择图片时是否处理好这种情况,或者是否有更好更简单的方法与第一种情况类似?

0 个答案:

没有答案