0

I am trying to send the image using the volley

 public class ImageSendJsonObjectHeader extends JsonRequest<JSONObject> {


       /* public ImageSendJsonObjectHeader (int method, String url, String requestBody, Response.Listener<JSONObject> listener, Response.ErrorListener errorListener) {
            super(method, url, requestBody, listener, errorListener);
        }
    */
        public ImageSendJsonObjectHeader (String url, JSONObject jsonRequest, Response.Listener<JSONObject> listener,
                                Response.ErrorListener errorListener) {
            this(jsonRequest == null ? Method.GET : Method.POST, url, jsonRequest,
                    listener, errorListener);
        }

        public ImageSendJsonObjectHeader (int method, String url, JSONObject jsonRequest,
                                Response.Listener<JSONObject> listener, Response.ErrorListener errorListener) {
            super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(), listener,
                    errorListener);

            // Log.i("Json Object",jsonRequest.toString());
        }

        @Override
        protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {

            try {
                //Log.i("Response parse","Yes");
                String jsonString = new String(response.data,
                        HttpHeaderParser.parseCharset(response.headers));
                //Log.i("Json String",jsonString);
                System.out.println("Json String is"+ jsonString);

                //Log.i("Response Complete",response.toString());
                //Log.i("Response Data",response.data.toString());
                return Response.success(new JSONObject(jsonString),
                        HttpHeaderParser.parseCacheHeaders(response));
            } catch (UnsupportedEncodingException e) {
                return Response.error(new ParseError(e));
            } catch (JSONException je) {
                return Response.error(new ParseError(je));
            }

        }


        @Override
        public String getBodyContentType() {
            return "form-data";
        }

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {

            //Log.i("Get Header","Enter");
            HashMap<String, String> headers = new HashMap<String, String>();
            //headers.put("Content-Type", "application/json; charset=utf-8");
            //headers.put("Content-Type", "application/x-www-form-urlencoded");
            Log.i("TOken is ", Constants.getTokenDB());
            //System.out.println("Token Length is "+Constants.getTokenDB().length());
            headers.put("x-access-token", Constants.getTokenDB());

            //Log.i("Get Header","Exit");
            return headers;

        }

    }

calling function:

private void uploadImage(File file){
        //Showing the progress dialog

        JSONObject jsonObject=new JSONObject();

        Uri path = Uri.fromFile(file);

        System.out.println("File is "+path);
        //String image=getStringImage(bt);

        System.out.println("Achaha "+path);
        try {
            jsonObject.put("Profile",path);
        }catch (JSONException js){
            js.printStackTrace();
        }


        Response.Listener listener=new Response.Listener<JSONObject>()
        {
            @Override
            public void onResponse(JSONObject response) {
                try {

                    Boolean responseCond=response.getBoolean("success");

                    System.out.println( "Response "+responseCond);

                    String imagePath=response.getString("url");
                    System.out.println("Image Path "+imagePath);
                }
                catch (JSONException k)
                {
                    Log.i("On Response",k.getMessage());
                    k.printStackTrace();
                }

            }

        };

        String image_url= Constants.url+"upload";

        imageSendJsonObjectHeader customRequest=new imageSendJsonObjectHeader(image_url,jsonObject, listener, Constants.errorListener);
        RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
        requestQueue.add(customRequest);


    }

Tried way:

  1. File file=new File(file_path);

  2. File file=new File(file_path); Uri path = Uri.fromFile(file);

  3. send the bitmap

  4. send the image as the string.

and pass the value in profile field.

Successful way using postman:

enter image description here

Jinesh Francis
  • 3,377
  • 3
  • 22
  • 37
Ankur Khandelwal
  • 1,042
  • 3
  • 19
  • 40
  • Hey @Ankur checkout this link it might help you http://stackoverflow.com/questions/16797468/how-to-send-a-multipart-form-data-post-in-android-with-volley?noredirect=1&lq=1 – Piyush Patel Dec 28 '16 at 07:53
  • hey @PiyushPatel while creating the class. I got the error while creating the multipartEntity class object. I think it need to pass the argument part[], HttpParams – Ankur Khandelwal Dec 28 '16 at 08:03
  • Hey @Ankur for Multipart http://stackoverflow.com/questions/28470486/android-multipartentity-and-dependencies – Piyush Patel Dec 28 '16 at 08:08
  • @PiyushPatel get two error Error:(43, 15) error: cannot access HttpEntity class file for org.apache.http.HttpEntity not found Error:(57, 37) error: cannot access Header class file for org.apache.http.Header not found – Ankur Khandelwal Dec 28 '16 at 08:28

2 Answers2

0

make it to base64 String

public static String imgToBase64(Bitmap bitmap) {
    ByteArrayOutputStream out = null;
    try {
        out = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 50, out);
        out.flush();
        out.close();
        byte[] imgBytes = out.toByteArray();
        return Base64.encodeToString(imgBytes, Base64.DEFAULT);
    } catch (Exception e) {
        return null;
    } finally {
        try {
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Thinsky
  • 4,226
  • 3
  • 13
  • 22
0

@Ankur use lagecy in build.gradle useLibrary 'org.apache.http.legacy'

Piyush Patel
  • 371
  • 1
  • 5
  • 13