Android : Upload image via PHP

on Friday, December 12, 2014


I need to upload image to server, but I am not good in PHP.


So can anyone help me with image.php ?


And it will be great if it bring me link to the uploaded image, like "/uploads/imgname.jpg". Something like that i guess:



$flag['link']=$link;
print(json_encode($flag));


This is my sending part:



private class ImageUploader extends AsyncTask<Void, Void, String> {

@Override
protected String doInBackground(Void... params) {
// TODO Auto-generated method stub
String result = "";

// Client-side HTTP transport library
HttpClient httpClient = new DefaultHttpClient();

// using POST method
HttpPost httpPostRequest = new HttpPost("http://10.0.2.2:4492/image.php");
try {

// creating a file body consisting of the file that we want to
// send to the server
File imageFile = new File(filePath);
FileBody bin = new FileBody(imageFile);

MultipartEntityBuilder multiPartEntityBuilder = MultipartEntityBuilder.create();
multiPartEntityBuilder.addPart("images[1]", bin);
httpPostRequest.setEntity(multiPartEntityBuilder.build());

// Execute POST request to the given URL
HttpResponse httpResponse = null;
httpResponse = httpClient.execute(httpPostRequest);

// receive response as inputStream
InputStream inputStream = null;
inputStream = httpResponse.getEntity().getContent();

if (inputStream != null)
result = convertInputStreamToString(inputStream);
else
result = "Did not work!";
return result;
} catch (Exception e) {

return null;
}

}

0 comments:

Post a Comment