用相机拍照然后上传到mysql数据库

时间:2018-05-05 12:05:17

标签: android mysql database android-studio camera

我试图制作一个应用程序从相机拍照并将其发送到Mysql数据库, 我的Android工作室代码存在问题。当我在模拟器中执行我的应用程序时  工作得很好,但如果我在我的手机中执行图片不上传到服务器这里是我的代码。

public class MainActivity extends AppCompatActivity {
private Button button;
private String encoded_string, image_name;
private Bitmap bitmap;
private File file;
private Uri file_uri;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    button = (Button) findViewById(R.id.start);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            getFileUri();
            i.putExtra(MediaStore.EXTRA_OUTPUT, file_uri);
            startActivityForResult(i, 10);
        }
    });
}

private void getFileUri() {
    image_name = "testing12345.jpg";
    file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
            + File.separator + image_name
    );

    file_uri = Uri.fromFile(file);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == 10 && resultCode == RESULT_OK) {
        new Encode_image().execute();



    }
}

private class Encode_image extends AsyncTask<Void, Void, Void> {
    @Override
    protected Void doInBackground(Void... voids) {

        bitmap = BitmapFactory.decodeFile(file_uri.getPath());
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
        bitmap.recycle();

        byte[] array = stream.toByteArray();
        encoded_string = Base64.encodeToString(array, 0);
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        makeRequest();
    }
}

private void makeRequest() {
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    StringRequest request = new StringRequest(Request.Method.POST, "http://tetsliens.esy.es/PHP_SCRIPT.php",
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {

                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    }) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            HashMap<String,String> map = new HashMap<>();
            map.put("encoded_string",encoded_string);
            map.put("image_name",image_name);

            return map;
        }
    };
    requestQueue.add(request);
}}

这是php脚本:

 <?php

header('Content-type:bitmap; charset = utf-8');

如果(isset($ _ POST [ “encoded_string”])){

$encoded_string = $_POST["encoded_string"];
$image_name = $_POST["image_name"];

$decoded_string = base64_decode($encoded_string);

$path = 'images/'.$image_name;

$file = fopen($path, 'wb');

$is_written = fwrite($file, $decoded_string);
fclose($file);

if($is_written > 0) {

    $connection = mysqli_connect('localhost', 'root','','upload');
    $query = "INSERT INTO photos(name,path) values('$image_name','$path');";

    $result = mysqli_query($connection, $query) ;

    if($result){
        echo "success";
    }else{
        echo "failed";
    }

    mysqli_close($connection);
}}?>

谢谢你的帮助

0 个答案:

没有答案