将图像android上传到mysql时出错

时间:2018-05-19 14:58:34

标签: php android mysql

将图片从Android应用上传到MySQL服务器时出错。

我已经按照教程,但无法完成此任务。 使用InnoDB表有4列(id,firstname,lastname,image)。 因此,当我运行我的应用程序并开始输入我的edittext时,单击保存,它将工作并将数据存储到数据库中。 我的观点是:如何使用我的代码将图像上传到数据库中。?

Signature.java

                @Override
                protected Map<String, String> getParams() throws AuthFailureError {
                    Map<String, String> parameters = new HashMap<String, String>();


                    File directory = Environment
                            .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
                    File file = new File(directory, System.currentTimeMillis() + ".JPEG");
                    FileOutputStream out = null;
                    Bitmap bitmap = signatureView.getSignatureBitmap();

                    try {
                        out = new FileOutputStream(file);
                        if (bitmap != null) {
                            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
                            ByteArrayOutputStream bytedata = new ByteArrayOutputStream();
                            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytedata);
                            byte[] data = bytedata.toByteArray();
                            String imagedata = Base64.encodeToString(data, Base64.DEFAULT);
                            String name="prescription";


                        } else {
                            throw new FileNotFoundException();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    parameters.put("firstname", txt1.getText().toString());
                    parameters.put("lastname", txt2.getText().toString());
                    return parameters;
                }
            };
            requestQueue.add(request);
        }
    });

connection.php

<?php
define('hostname', 'localhost');
define('user', 'root');
define('password', '');
define('databaseName', 'tutorial'); 
$connect = mysqli_connect(hostname, user, password, databaseName);
mysqli_set_charset($connect,"utf8");
?>

insert.php

<?php
if($_SERVER["REQUEST_METHOD"]=="POST"){
    require 'connection.php';
    createStudent();
}
function createStudent()
{
    global $connect;
    $firstname = $_POST["firstname"];   
    $lastname = $_POST["lastname"];
    $query = " Insert into students(firstname,lastname) values ('$firstname','$lastname');";
    mysqli_query($connect, $query) or die (mysqli_error($connect));
    mysqli_close($connect);
}
?>

0 个答案:

没有答案