Search code examples
androidmultipart

How to add multiple Mime types for volley MultipartEntityBuilder


I have to upload image/audio/video/pdf...etc files to my server using volley MultipartEntityBuilder and i am using below code for Mimetype setup but i am getting exception like below

Can some one help me please how can i add multiple Mime types?

code:

    /**
     *buildMultipartEntity
     */
    private void buildMultipartEntity() {

        if (mParams != null) {
            for (Map.Entry<String, String> entry : mParams.entrySet()) {
                mBuilder.addTextBody(entry.getKey(), entry.getValue());
            }
        }

        if (mFileParams != null) {

            for (Map.Entry<String, List<File>> entry : mFileParams.entrySet()) {

                List<File> listFiles = entry.getValue();

                for (File file : listFiles) {

                        String[] mimeTypes = {"image/*", "application/pdf"};
                        mBuilder.addBinaryBody(entry.getKey(), file, ContentType.create(String.valueOf(mimeTypes),""),
                                file.getName());

                }
            }
        }
        mBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
    }
}

Error:

java.lang.IllegalArgumentException: MIME type may not contain reserved characters
2018-11-29 11:14:52.955 18868-18868/com.karvyinnotech.claimsportal W/System.err:     at org.apache.http.util.Args.check(Args.java:36)
2018-11-29 11:14:52.955 18868-18868/com.karvyinnotech.claimsportal W/System.err:     at org.apache.http.entity.ContentType.create(ContentType.java:176)
2018-11-29 11:14:52.955 18868-18868/com.karvyinnotech.claimsportal W/System.err:     at org.apache.http.entity.ContentType.create(ContentType.java:204)
2018-11-29 11:14:52.955 18868-18868/com.karvyinnotech.claimsportal W/System.err:     at com.karvyinnotech.claimsportal.restapi.MultipartAPI.buildMultipartEntity(MultipartAPI

Solution

  • use mime type "*/*" it will accept all types of files

    var fileBody = ProgressRequestBody(fileToUpload, "*/*", this)
    
            photos = MultipartBody.Part.createFormData(key, fileToUpload.name, fileBody)
    

    Like in retrofit I make RequestBody object as above and it accepts any type of file