CodeAssasins CodeAssasins - 1 year ago 106
Android Question

upload image and other params to server

I'm updating image and some text to server using volley but the image is not getting uploaded. I searched for it and found that images need to be send using multipart and volley doesn't support multipart. Is there any way to use volley to upload image and texts?

// Volley request

StringRequest stringRequest = new StringRequest(Request.Method.POST, URLUtils.URL,
new Response.Listener<String>() {
public void onResponse(String response) {

try {
JSONObject jObj = new JSONObject(response);
Toast.makeText(getApplicationContext(), jObj.getString("msg"), Toast.LENGTH_SHORT).show();

} catch (JSONException e) {
// JSON error
Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}, new Response.ErrorListener() {
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), "VolleyError" + error.toString(), Toast.LENGTH_LONG).show();
}) {
protected Map<String, String> getParams() {

Map<String, String> params = new HashMap<String, String>();
params.put("auth", "abc167");
params.put("action", "editprofile");
params.put("first_name", fname_et.getText().toString());
params.put("last_name", lname_et.getText().toString());
params.put("address", address_str);
params.put("zipcode", zip_et.getText().toString());
params.put("phoneno", phone_et.getText().toString());
params.put("device_token_id", "1234567890");
params.put("device_type", "android");
params.put("lat", lat_str);
params.put("long", lng_str);
params.put("photo", imagepath);
params.put("userid", "121");

return params;


RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());

Answer Source

Try this way:

class ImageUploadTask extends AsyncTask<String, Void, String> {

        protected void onPreExecute() {
            // TODO Auto-generated method stub

        protected String doInBackground(String... params) {
            try {

                String url = URLUtils.URL;

                HttpClient httpClient = new DefaultHttpClient();
                //HttpContext localContext = new BasicHttpContext();
                HttpPost httpPost = new HttpPost(url);
                MultipartEntity entity = new MultipartEntity();

                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
                byte[] data = bos.toByteArray();

                entity.addPart("auth", new StringBody("abc167"));
                entity.addPart("action", new StringBody("editprofile"));
                entity.addPart("first_name", new StringBody(fname));
                entity.addPart("last_name", new StringBody(lname));
                entity.addPart("address", new StringBody(address_str));
                entity.addPart("zipcode", new StringBody(zip));
                entity.addPart("phoneno", new StringBody(phone));
                entity.addPart("device_token_id", new StringBody("1234567890"));
                entity.addPart("device_type", new StringBody("android"));
                entity.addPart("lat", new StringBody(lat_str));
                entity.addPart("long", new StringBody(lng_str));
                entity.addPart("userid", new StringBody("121"));
                entity.addPart("photo", new ByteArrayBody(data, "myimage.jpg"));

                HttpResponse response = httpClient.execute(httpPost);
                BufferedReader reader = new BufferedReader(new InputStreamReader(
                        response.getEntity().getContent(), "UTF-8"));
                String sResponse;
                 s = new StringBuilder();

                while ((sResponse = reader.readLine()) != null) {
                    s = s.append(sResponse);
                System.out.println("Response: " + s);

            } catch (Exception e) {

                Log.e(e.getClass().getName(), e.getMessage(), e);

            return s.toString();

        protected void onPostExecute(String sResponse) {
            try {

                if (sResponse != null) {
                            sResponse + " Photo uploaded successfully",


            } catch (Exception e) {
                Toast.makeText(getApplicationContext(), e.getMessage(),
                Log.e(e.getClass().getName(), e.getMessage(), e);


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download