0

I want to send file (like jpg,pdf etc)in backend instead of file path using Android volley. I am getting file path instead of file in backend. We request you to please tell me the procedure how to send file in backend using file chooser instead of file path. Please tell me the procedure.

   // Submit btn  
        Sbmt_btn.setOnClickListener (new View.OnClickListener ( ) {


             @Override
             public void onClick(View view) {
                 NextAdditional();
             }

             private void NextAdditional() {

                 JSONObject js = new JSONObject ();
                 try {
                     js.put ("extraInfoId","");
                     js.put ("createdBy","");
                     js.put ("partyId", "");
                     js.put ("notes",Note_Edit.getText ().toString ());
                     js.put ("logoPath","");
                     JSONObject jsonObject_one = new JSONObject ();
                     jsonObject_one.put ("id","");
                     jsonObject_one.put ("certificationType", "");
                     jsonObject_one.put ("certificationfilepath", b);
                     JSONObject jsonObject_two = new JSONObject ();
                     jsonObject_two.put ("id", "");
                     jsonObject_two.put ("documentType","");
                     jsonObject_two.put ("documentfilepath","");
                     JSONArray ja = new JSONArray ();
                     ja.put (jsonObject_one);
                     js.put ("certifications",ja);
                     JSONArray jsonArray = new JSONArray ();
                     jsonArray.put (jsonObject_two);
                     js.put ("documents",jsonArray);

                 } catch (JSONException e) {
                     e.printStackTrace ( );
                 }

                 mrequestqueue = Volley.newRequestQueue (AdditionalActivity.this);

                         JsonObjectRequest jsonObjReq = new JsonObjectRequest (
                                 Request.Method.POST,url, js,
                                 new Response.Listener<JSONObject>() {
                                     @Override
                                     public void onResponse(JSONObject response) {
                                         Log.d(TAG, response.toString());

                                         Toast.makeText (AdditionalActivity.this,"Success",Toast.LENGTH_LONG).show ();


                                     }
                                 }, new Response.ErrorListener() {

                             @Override
                             public void onErrorResponse(VolleyError error) {
                                 Log.d(TAG, "Error: " + error.getMessage());
                                 Toast.makeText (AdditionalActivity.this,"Failure",Toast.LENGTH_LONG).show ();

                             }
                         }) {

                             /**
                              * Passing some request headers
                              */
                             @Override
                             public Map <String, String> getHeaders() throws AuthFailureError {
                                 HashMap <String, String> headers = new HashMap <String, String> ( );
                                 headers.put ("Content-Type", "application/json; charset=utf-8");
                                 return headers;
                             }

                         };

                         mrequestqueue.add (jsonObjReq);

                     }

         });

// Attachement 1

         ISO_btn.setOnClickListener (new View.OnClickListener ( ) {
             @Override
             public void onClick(View view) {

                 new MaterialFilePicker()
                         .withActivity(AdditionalActivity.this)
                         .withRequestCode(1003)
                         .withHiddenFiles(true) // Show hidden files and folders
                         .start();


             }
         });


         Logo_btn.setOnClickListener (new View.OnClickListener ( ) {
             @Override
             public void onClick(View view) {

                 new MaterialFilePicker()
                         .withActivity(AdditionalActivity.this)
                         .withRequestCode(1002)
                         .withHiddenFiles(true) // Show hidden files and folders
                         .start();

             }
         });





         Attachment_btn.setOnClickListener (new View.OnClickListener ( ) {
             @Override
             public void onClick(View view) {
                  new MaterialFilePicker()
                         .withActivity(AdditionalActivity.this)
                         .withRequestCode(1000)
                         .withHiddenFiles(true) // Show hidden files and folders
                         .start();
             }
         });


            }

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

                if (requestCode == 1000 && resultCode == RESULT_OK) {
                    String filePath = data.getStringExtra(FilePickerActivity.RESULT_FILE_PATH);
                     Attachment_tv.setText (filePath);
                     file = new File (filePath);

                    b = new byte[(int) file.length()];
                    try {
                        FileInputStream fileInputStream = new FileInputStream(file);
                        fileInputStream.read(b);
                        for (int i = 0; i < b.length; i++) {
                            System.out.print((char)b[i]);
                        }
                    } catch (FileNotFoundException e) {
                        System.out.println("File Not Found.");
                        e.printStackTrace();
                    }
                    catch (IOException e1) {
                        System.out.println("Error Reading The File.");
                        e1.printStackTrace();
                    }


                }

                if (requestCode == 1002 && resultCode == RESULT_OK) {
                    String filePath1 = data.getStringExtra(FilePickerActivity.RESULT_FILE_PATH);
                    Logo_TV.setText (filePath1);
                    // Do anything with file
                }

                if (requestCode == 1003 && resultCode == RESULT_OK) {
                    String filePath2 = data.getStringExtra(FilePickerActivity.RESULT_FILE_PATH);
                    ISO_TV.setText (filePath2);
                    // Do anything with file
                }

            }

            @Override
            public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
                switch (requestCode){
                    case 1001:{
                        if (grantResults[0]==PackageManager.PERMISSION_GRANTED){

                            Toast.makeText (this,"PERMISSION GRANTED",Toast.LENGTH_LONG).show ();
                        } else {
                            Toast.makeText (this,"PERMISSION DENIED",Toast.LENGTH_LONG).show ();
                            finish ();

                            {
                        }

                        }
                    }
                }
            }
        }
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • Take a look at [this](https://stackoverflow.com/a/16803473/1203122) for the Volley Request part. – xRed Jan 03 '18 at 14:24
  • https://www.androidlearning.com/multipart-request-using-android-volley/ "Link" give it a look, this example is using volley to upload files to server – Wajid Jan 03 '18 at 14:31

0 Answers0