G
G
Gfd2016-12-02 17:20:43
PHP
Gfd, 2016-12-02 17:20:43

How to send image to server (PHP) from android?

You need to do the following:
1) the user selects an image from the gallery
2) this image is sent to the server
3) the image is stored on the
Android server:

private class ConnectToServer extends AsyncTask<Void, Void, Integer> {

        HttpURLConnection conn;
        Integer res;

        protected Integer doInBackground(Void... params) {

            try {
                Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
                photoPickerIntent.setType("image/*");
                startActivityForResult(photoPickerIntent, 1);


                HttpRequest request = HttpRequest.post("http://potatosing.16mb.com/index.php");
                request.part("image", "image.jpg", new File(mUri.getPath()));


                URL url = new URL(request.body());
                conn = (HttpURLConnection) url.openConnection();
                conn.setConnectTimeout(1000000);
                conn.setRequestMethod("POST");
                conn.setRequestProperty("User-Agent", "Mozilla/5.0");
                conn.connect();

                Toast.makeText(getApplicationContext(), url.getFile(), Toast.LENGTH_SHORT).show();
            } catch (Exception e) {

            }
            return res;
        }
    }

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
        super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

        if (resultCode == RESULT_OK) {
            mUri = imageReturnedIntent.getData();
        }

    }
}

In this line:
request.part("image", "image.jpg", new File(mUri.getPath()));

getPath() returns null
Actually the questions are:
1) why it returns null
2) how to send this image correctly
3) how to "accept" this image in php and save it
PS: To make a post request I used: https://github.com/kevinsawicki/ http-request
PSS: I'm completely new, and please be understanding)
Thanks in advance)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question