Problem uploading a photo to a web server

Asked

Viewed 96 times

0

Hi! I’m having a problem uploading a photo taken from an android app to a web server. On the local server it usually sends the photo, already on the web server, it sends only one photo and only sends another if it clears the application cache on android. Below is the code responsible for taking the photo and sending to the server. Thanks to those who can help.

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

private void getFileUri() {

    image_name = "teste123.jpeg";
    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://teste.com.br/servidor/package/photo/connection.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);
    Toast.makeText(CarActivity.this, "Foto enviada com sucesso.", Toast.LENGTH_SHORT).show();
}
  • Dude, maybe it’s because you’re putting the name fixed to the image. So when you take a new picture, it will say that the image already exists. Try to define the image name dynamically, for example, enter the current time you took the photo and put it as the image name.

  • I already tried that and it didn’t work. And also if that were it would not work locally, where in this case it replaces the photo because it already has one with the same name.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.