How to make multipart/form-data request using Volley in Android?
<p>I am developing an Android app. Now , I am having a problem with file upload. I have a function in backend that create item with image. I am using repository pattern, so I can share functions to API. Before I am uploading file in this way.</p>

<p>My activity for file upload:</p>

<pre><code>public class CreateItemActivity extends Activity {
    Button btnChose;
    Button btnUpload;
    Bitmap bitmap;
    ImageView imageView;
    private String KEY_IMAGE = "image";
    private String KEY_NAME = "name";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.create_item);
        btnChose = (Button)findViewById(R.id.buttonChoose);
        btnUpload = (Button)findViewById(R.id.buttonUpload);
        btnChose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showFileChooser();
            }
        });
        imageView = (ImageView)findViewById(R.id.imageView);
        btnUpload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                uploadImage();
            }
        });
    }

    private void showFileChooser()
    {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Choose image"), 1);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 &amp;&amp; resultCode == RESULT_OK &amp;&amp; data != null &amp;&amp; data.getData() != null) {
            Uri filePath = data.getData();
            try {
                //Getting the Bitmap from Gallery
                bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
                //Setting the Bitmap to ImageView
                imageView.setImageBitmap(bitmap);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public String getStringImage(Bitmap bmp){
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.PNG, 100, baos);
        byte[] imageBytes = baos.toByteArray();
        String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
        return encodedImage;
    }

    private void uploadImage(){
        //Showing the progress dialog
        final ProgressDialog loading = ProgressDialog.show(this,"Uploading...","Please wait...",false,false);
        StringRequest stringRequest = new StringRequest(Request.Method.POST, getResources().getString(R.string.api_end_point)+"android-upload",
                new Response.Listener&lt;String&gt;() {
                    @Override
                    public void onResponse(String s) {
                        //Disimissing the progress dialog
                        loading.dismiss();
                        //Showing toast message of the response
                        Toast.makeText(getBaseContext(), "File uploaded" , Toast.LENGTH_LONG).show();
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        //Dismissing the progress dialog
                        loading.dismiss();

                        //Showing toast
                       Toast.makeText(getBaseContext(), "Error listener", Toast.LENGTH_LONG).show();
                    }
                }){
            @Override
            protected Map&lt;String, String&gt; getParams() throws AuthFailureError {
                //Converting Bitmap to String
                String image = getStringImage(bitmap);

                //Getting Image Name
                String name = "android_uploaded_image";

                //Creating parameters
                Map&lt;String,String&gt; params = new Hashtable&lt;String, String&gt;();

                //Adding parameters
                params.put(KEY_IMAGE, image);
                params.put(KEY_NAME, name);

                //returning parameters
                return params;
            }
        };

        //Creating a Request Queue
        RequestQueue requestQueue = Volley.newRequestQueue(this);

        //Adding request to the queue
        requestQueue.add(stringRequest);
    }
}'
</code></pre>

<p>That is just test activity for file upload. It works fine. It uploads file to server. But the problem is I have to upload file in server like below. I am using PHP for backend.</p>

<pre><code>&lt;?php file_put_contents(time().".png", base64_decode($_POST['image']); ?&gt;
</code></pre>

<p>As you can see above, I cannot retrieve file like <code>$_FILES['image']</code>. Instead, I have to use <code>$_POST</code>. My problem is I already have function for file upload in server  that uses <code>$_FILES</code>. So I am trying to make <code>multipart/form-data</code> request. I searched articles for it. But these are incomplete because some components in code cannot be used.</p>

<p>For example, for this link <a href="https://stackoverflow.com/questions/32187450/trouble-sending-multipart-file-with-boundary-via-volley">Trouble Sending Multipart File with Boundary via Volley</a>, I cannot use MultipartBuilder. How can I make multipart/form-data request easily using volley in android. I am using latest API version , so some functions and components cannot be used as in link I mentioned.</p>