Answer the question
In order to leave comments, you need to log in
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();
}
}
}
request.part("image", "image.jpg", new File(mUri.getPath()));
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question